This is a creation in Article, where the information may have evolved or changed.
Package definition format is length, checksum code and JSON content
Package Mainimport ("bytes" "Encoding/binary" "Encoding/json" "FMT" "Hash/crc32" "IO" "NET" "OS") type Register struct { ACTION int32sid int32}type Packet struct {length uint32crc32 uint32info string}func (P Packet) Encode () []byte {buf2 : = new (bytes. Buffer) var length int = Len ([]byte (P.info)) Err: = Binary. Write (buf2, Binary. Littleendian, (Int32) (length)) checkerror (err) err = binary. Write (buf2, Binary. Littleendian, []byte (P.info)) checkerror (err) BUF: = new (bytes. Buffer) P.length = UInt32 (buf2. Len () + 8) Err = binary. Write (buf, Binary. Littleendian, P.length) checkerror (err) P.crc32 = Crc32. Checksumieee (buf2. Bytes ()) Err = binary. Write (buf, Binary. Littleendian, P.CRC32) checkerror (err) err = binary. Write (buf, Binary. Littleendian, Buf2. Bytes ()) CheckError (ERR) return BUF. Bytes ()}func (P *packet) Decode (Buff []byte) {buf: = Bytes. Newbuffer (Buff) Err: = Binary. Read (buf, Binary. Littleendian, & (P.length)) checkerror (err) fmt. PRINTLN (p.length) err = binary. Read (buf, Binary. Littleendian, & (P.CRC32)) checkError (err) Buf2: = bytes. Newbuffer (buff[8:]) CRC: = Crc32. Checksumieee (buf2. Bytes ()) If CRC = P.crc32 {FMT. Errorf ("CRC not Check")}p.info = (string) (Buf2. Bytes ()) fmt. Printf ("%s", P.info)}func main () {m: = register{20004, 6}b, err: = json. Marshal (m) checkerror (ERR) var packet packetpacket.info = string (b) BUF: = packet. Encode () fmt. Println (Len (buf)) var msg packetmsg.decode (BUF) OS. Exit (0)}func checkerror (err error) {if err! = Nil {fmt. fprintf (OS. Stderr, "Fatal Error:%s", err. Error ()) OS. Exit (1)}}