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...
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...
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...
fmt.Println("Kind: ", t.Kind())fori :=0; i < t.NumField(); i++ {// Get the field, returns https://golang.org/pkg/reflect/#StructFieldfield := t.Field(i)//Get the field tag valuetag := field.Tag.Get(tagName) fmt.Printf("%d. %v(%v), tag:'%v'\n", i+1, field.N...
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 (泛型编程) ...
Struct: fmt.Printf(strings.Repeat("\t", depth)+"%v %v {\n", t.Name(), t.Kind()) for i := 0; i < v.NumField(); i++ { f := v.Field(i) if f.Kind() == reflect.Struct || f.Kind() == reflect.Ptr { fmt.Printf(strings.Repeat("\t", depth+1)+"%s %s : \n",...
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 ...
type Bstruct{ num1 uint64 num2 int64 } func(b*B)Add(){ b.num1++ b.num2=int64(b.num1/2) } typeAdderinterface{ Add() } funcDoAdd[TAdder](t T){ t.Add() } funcDoAddNoGeneric(aAdder){ a.Add() } funcBenchmarkNoGenericA(b*testing.B){ ...
type jsonEncoder struct { *EncoderConfig buf *buffer.Buffer spaced bool // include spaces after colons and commas openNamespaces int // for encoding generic values by reflection reflectBuf *buffer.Buffer reflectEnc *json.Encoder } jsonEncoder内嵌了EncoderConfig,定义了buf、spaced、openNamespaces、ref...
fieldValue := entityValue.FieldByName(field) if value == nil { // diff中value为nil,将fielValue设为默认值 fieldValue.Set(reflect.Zero(fieldValue.Type())) } else { fieldValue.Set(reflect.ValueOf(value)) } } } func main() { input := &testStruct{A:10, B:"hello", C:3.14} ...