funcBenchmarkGenericInterfaceA(b *testing.B){varobj Adder = &A{}fori :=0; i < b.N; i++ {DoAdd(obj)}} funcBenchmarkGenericInterfaceB(b *testing.B){varobj Adder = &B{}fori :=0; i < b.N; i++ {DoAdd(obj)}} funcBenchmarkDoAddNo...
typeAstruct{ numuint64 num1int64 } func(a *A)Add() { a.num++ a.num1 =int64(a.num /2) } typeBstruct{ num1uint64 num2int64 } func(b *B)Add() { b.num1++ b.num2 =int64(b.num1 /2) } typeAdderinterface{ Add() } funcDoAdd[TAdder](t T){ t.Add() } funcDoAddNoGen...
struct 是一个class但是没有virtual方法,比如说: type Rectangle struct { Name string Width, Height float64 } func(r Rectangle) Area() float64 { return r.Width + r.Height } 那么伪代码如下: class Rectangle field Name: string field Width: float64 field Height: float64 method Area() //non-...
typeUserstruct{FirstNamestringLastNamestringEmailstring}funcUserBy[Vany](users[]User,fieldstructfield[User],valueV)User{index:=slices.IndexFunc(users,func(uUser)bool{returnuser.(field)==value})ifindex==-1{returnUser{} }returnusers[index] }funcBy[Sany,Vany](items[]S,field,structfield[S],v...
// Use the html/template package instead, which automatically escapes output to prevent XSS attackstmpl:=htmltemplate.Must(htmltemplate.New("safe").Parse(`Hello, {{.Name}}!`))data:=struct{Name string}{Name:"alert('XSS');"}err:=tmpl.Execute(os.Stdout,data)iferr!=nil{log.Fatal(err)...
Kind:struct1.Id(int), tag:'-'2.Name(string), tag:'presence,min=2,max=32'3.Email(string), tag:'email,required' 通过reflect包我们能够获取User结构体id基本信息,包括它的类型、种类且能列出它的所有字段。如你所见,我们打印了每个字段的标签。标签没有什么神奇的地方,field.Tag.Get方法返回与标签名匹...
func (me MyStruct) Print() {} func main() { var me MyStruct TestFunc(me) } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. Why Interface 为什么要用接口呢?在Gopher China 上的分享中,有大神给出了下面的理由: writing generic algorithm (泛型编程) ...
Field(i).Interface() } } return data } func TestS(t *testing.T) { obj := CommonObj{ Name: "aa", Age: 12, LastUpdateTime: time.Now(), score: 1.2, } m := StructConvertMapByTag(obj, "persistence") fmt.Println(m) bytes, _ := json.Marshal(obj) fmt.Println(string(bytes)) ...
Validate the presence of the field in a struct in Golang Validate the range of the integer in a struct in Golang Time All about time and date in Go Represent date of birth in golang. Get age given a dob Current Timestamp in Go ...
Loukoum is a simple SQL Query Buildergosql based on sqlx, It's simple and keep simplego-tagexpr An interesting go struct tag expression syntax for field validation, etc.graceful reload golang http server, zero downtime, compatible with systemd, supervisorgo-daemon Build Status GoDoc Library ...