package main import ( "bytes" "encoding/binary" "fmt" ) type Register struct { ACTION int32 SID int32 } func ExampleWrite() []byte { buf := new(bytes.Buffer) var info Register info.ACTION = 20004 info.SID = 6 err := binary.Write(buf, binary.LittleEndian, info) if err != nil { fmt.Println("binary.Write failed:", err) } fmt.Printf("% x\n", buf.Bytes()) return buf.Bytes() } func ExampleRead(b []byte) { var info Register buf := bytes.NewBuffer(b) err := binary.Read(buf, binary.LittleEndian, &info) if err != nil { fmt.Println("binary.Read failed:", err) } fmt.Print(info) // Output: 3.141592653589793 } func main() { buf := ExampleWrite() ExampleRead(buf) }
go语言标准库除了提供了binary的之外,还提供了json,gob以及base32,base64的序列化
对于protocol buff协议的序列化,可以参见goprotobuf
有疑问加站长微信联系(非本文作者)