github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/common/hexutil/json_example_test.go (about) 1 package hexutil_test 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/quickchainproject/quickchain/common/hexutil" 8 ) 9 10 type MyType [5]byte 11 12 func (v *MyType) UnmarshalText(input []byte) error { 13 return hexutil.UnmarshalFixedText("MyType", input, v[:]) 14 } 15 16 func (v MyType) String() string { 17 return hexutil.Bytes(v[:]).String() 18 } 19 20 func ExampleUnmarshalFixedText() { 21 var v1, v2 MyType 22 fmt.Println("v1 error:", json.Unmarshal([]byte(`"0x01"`), &v1)) 23 fmt.Println("v2 error:", json.Unmarshal([]byte(`"0x0101010101"`), &v2)) 24 fmt.Println("v2:", v2) 25 // Output: 26 // v1 error: hex string has length 2, want 10 for MyType 27 // v2 error: <nil> 28 // v2: 0x0101010101 29 }