多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# package encoding `import "encoding"` encoding包定义了供其它包使用的可以将数据在字节水平和文本表示之间转换的接口。encoding/gob、encoding/json、encoding/xml三个包都会检查使用这些接口。因此,只要实现了这些接口一次,就可以在多个包里使用。标准包内建类型time.Time和net.IP都实现了这些接口。接口是成对的,分别产生和还原编码后的数据。 ## Index * [type BinaryMarshaler](#BinaryMarshaler) * [type BinaryUnmarshaler](#BinaryUnmarshaler) * [type TextMarshaler](#TextMarshaler) * [type TextUnmarshaler](#TextUnmarshaler) ## type [BinaryMarshaler](https://github.com/golang/go/blob/master/src/encoding/encoding.go#L18 "View Source") ``` type BinaryMarshaler interface { MarshalBinary() (data []byte, err error) } ``` 实现了BinaryMarshaler接口的类型可以将自身序列化为binary格式。 ## type [BinaryUnmarshaler](https://github.com/golang/go/blob/master/src/encoding/encoding.go#L28 "View Source") ``` type BinaryUnmarshaler interface { UnmarshalBinary(data []byte) error } ``` 实现了BinaryUnmarshaler接口的类型可以将binary格式表示的自身解序列化。 UnmarshalBinary必须可以解码MarshalBinary生成的binary格式数据。本函数可能会对data内容作出修改,所以如果要保持data的数据请事先进行拷贝。 ## type [TextMarshaler](https://github.com/golang/go/blob/master/src/encoding/encoding.go#L36 "View Source") ``` type TextMarshaler interface { MarshalText() (text []byte, err error) } ``` 实现了BinaryMarshaler接口的类型可以将自身序列化为utf-8编码的textual格式。 ## type [TextUnmarshaler](https://github.com/golang/go/blob/master/src/encoding/encoding.go#L46 "View Source") ``` type TextUnmarshaler interface { UnmarshalText(text []byte) error } ``` 实现了TextUnmarshaler接口的类型可以将textual格式表示的自身解序列化。 UnmarshalText必须可以解码MarshalText生成的textual格式数据。本函数可能会对data内容作出修改,所以如果要保持data的数据请事先进行拷贝。