github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/common/hexutil/json_example_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:32</date> 10 //</624342609225060352> 11 12 13 package hexutil_test 14 15 import ( 16 "encoding/json" 17 "fmt" 18 19 "github.com/ethereum/go-ethereum/common/hexutil" 20 ) 21 22 type MyType [5]byte 23 24 func (v *MyType) UnmarshalText(input []byte) error { 25 return hexutil.UnmarshalFixedText("MyType", input, v[:]) 26 } 27 28 func (v MyType) String() string { 29 return hexutil.Bytes(v[:]).String() 30 } 31 32 func ExampleUnmarshalFixedText() { 33 var v1, v2 MyType 34 fmt.Println("v1 error:", json.Unmarshal([]byte(`"0x01"`), &v1)) 35 fmt.Println("v2 error:", json.Unmarshal([]byte(`"0x0101010101"`), &v2)) 36 fmt.Println("v2:", v2) 37 //输出: 38 //v1错误:十六进制字符串的长度为2,MyType需要10 39 //V2错误:<nIL> 40 //版本2:0x0101010101 41 } 42