github.com/philpearl/plenc@v0.0.15/example_test.go (about) 1 package plenc_test 2 3 import ( 4 "fmt" 5 6 "github.com/philpearl/plenc" 7 ) 8 9 func Example() { 10 type Hat struct { 11 Type string `plenc:"1"` 12 Size float32 `plenc:"2"` 13 } 14 15 type Person struct { 16 Name string `plenc:"1"` 17 Age int `plenc:"2"` 18 Hats []Hat `plenc:"3"` 19 } 20 21 var me = Person{ 22 Name: "Lucy", 23 Age: 25, 24 Hats: []Hat{ 25 {Type: "Fedora", Size: 6}, 26 {Type: "floppy", Size: 5.5}, 27 }, 28 } 29 30 data, err := plenc.Marshal(nil, &me) 31 if err != nil { 32 fmt.Println(err) 33 return 34 } 35 36 fmt.Printf("%X\n", data) 37 38 var out Person 39 if err := plenc.Unmarshal(data, &out); err != nil { 40 fmt.Println(err) 41 return 42 } 43 44 fmt.Printf("%#v\n", out) 45 46 // Output: 0A044C75637910321B020D0A064665646F7261150000C0400D0A06666C6F707079150000B040 47 // plenc_test.Person{Name:"Lucy", Age:25, Hats:[]plenc_test.Hat{plenc_test.Hat{Type:"Fedora", Size:6}, plenc_test.Hat{Type:"floppy", Size:5.5}}} 48 }