github.com/xhebox/bstruct@v0.0.0-20221115052913-86d4d6d98866/example/main.go (about) 1 package main 2 3 //go:generate go run . -o gen.go -p main 4 //go:generate go fmt . 5 6 import ( 7 "bytes" 8 "flag" 9 "log" 10 "os" 11 12 . "github.com/xhebox/bstruct" 13 ) 14 15 func main() { 16 out := flag.String("o", "gen.go", "output file") 17 pak := flag.String("p", "bench", "package name") 18 flag.Parse() 19 20 buf := new(bytes.Buffer) 21 enc := NewBuilder().Getter(true).Setter(true) 22 New(FieldStruct). 23 Reg(enc, "Struct1"). 24 Comment("Struct1 is fff"). 25 Add("A", "gfgf", false, New(FieldBool)). 26 Add("B", "", false, NewSlice(New(FieldBool))). 27 Add("C", "", false, 28 New(FieldStruct). 29 Reg(enc, "Struct2"). 30 Add("A", "", false, New(FieldBool)), 31 ). 32 Add("D", "", false, NewString()). 33 Add("G", "", false, NewSlice(NewString())). 34 Add("E", "", false, 35 NewSlice(New(FieldStruct). 36 Reg(enc, "Slice1"). 37 Add("E", "", false, NewString())), 38 ). 39 Add("fieldGerrrccontrol", "", true, New(FieldBool)) 40 enc.Process() 41 enc.Print(buf, *pak) 42 if err := os.WriteFile(*out, buf.Bytes(), 0644); err != nil { 43 log.Fatal(err) 44 } 45 }