This is a creation in Article, where the information may have evolved or changed.
struct to BUF
package mainimport ("bytes" "Encoding/binary" "Reflect" "IO" "Errors" "FMT") func writebuf (W io. Writer,v reflect. Value) (n int, err error) {newbuf := bytes. Newbuffer (nil) For i := 0; i < v.numfield (); i++ {switch v. Field (i). Type (). Kind () {case reflect. Struct:n,err := writebuf (Newbuf,v.field (i)) if err != nil {return n,err}case reflect. Bool:boolbyte := []byte{0}if v.field (i). Bool () {boolbyte = []byte{1}}newbuf.write (boolbyte) case reflect. String:newBuf.WriteString (V.field (i). String ()) Case reflect. Slice:newBuf.Write (V.field (i). Bytes ()) Case reflect. Int:binary. Write (newbuf, binary. Littleendian,int32 (V.field (i). Int ())) Case reflect. Uint:binary. Write (newbuf, binary. Littleendian, uint32 (V.field (i). Uint ())) Case reflect. Uint8, reflect. Uint16, reflect. Uint32, reflect. Uint64,reflect. Int8, reflect. Int16, reflect. Int32, reflect. Int64,reflect. Float32, reflect. Float64, reflect.complex64, reflect.complex128:binary.write (newbuf, binary. Littleendian, v.field (i). Interface ())}}return w.write (Newbuf.bytes ())}func writestructtobuffer (W io. writer ,data interface{}) ( error ) {v := reflect. Indirect (reflect. ValueOf (data) If v.kind () == reflect. Struct {fmt. PRINTLN ("Test") _,err := writebuf (w,v) return err}return errors. New ("Invalid type not a struct")}func stringfixedlength (s string, length int) []byte {slength := len (s) if slength >= length {return []byte (S[:length])} else {b := make ([]byte, length-slength) Return append ([ ]byte (s), b ...)} Return nil}