if a == nil {
b = nil
} else {
b = make([]T, len(a))
copy(b, a)
}
ベンチマーク
テストコード
package main
import (
"testing"
)
const N = 1024 * 1024
type T = int64
var xForMakeCopy = make([]T, N)
var xForAppend = make([]T, N)
var yForMakeCopy []T
var yForAppend []T
func Benchmark_MakeAndCopy(b *testing.B) {
for i := 0; i < b.N; i++ {
yForMakeCopy = make([]T, N)
copy(yForMakeCopy, xForMakeCopy)
}
}
func Benchmark_Append(b *testing.B) {
for i := 0; i < b.N; i++ {
yForAppend = append(xForAppend[:0:0], xForAppend...)
}
}
テスト結果
❯ go version
go version go1.14.2 darwin/amd64
❯ go test -bench=.
goos: darwin
goarch: amd64
Benchmark_MakeAndCopy-8 1142 990016 ns/op
Benchmark_Append-8 2289 526206 ns/op
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "My site",
})
文字列をHTMLとして返すメソッドは用意されてないようです。
解決策
//Do what you need to get the cached html
yourHtmlString := "<html><body>I am cached HTML!</body></html>"
//Write your 200 header status (or other status codes, but only WriteHeader once)
c.Writer.WriteHeader(http.StatusOK)
//Convert your cached html string to byte array
c.Writer.Write([]byte(yourHtmlString))
t := reflect.TypeOf(people)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tag := field.Tag.Get("json") // `json`タグ情報を取得
}
下記のように、複数のタグはスペースで区切って記述できます。
type People struct {
Name string `json:"name" validate:"required"`
Sex string `json:"sex" validate:"required"`
Age int `json:"age" validate:"required"`
Height int `json:"height" validate:"required"`
Weight int `json:"weight" validate:"required"`
}