import ( "github.com/tinylib/msgp/msgp" "bytes" ) func main() { var buf bytes.Buffer writer := msgp.NewWriter(&buf) num := int64(1234567890) err := writer.WriteInt64(num) if err != nil { panic(err) } writer.Flush() }
type Person struct { Name string Age int64 } func encodePerson(p *Person) ([]byte, error) { var buf bytes.Buffer writer := msgp.NewWriter(&buf) err := writer.WriteString(p.Name) if err != nil { return nil, err } err = writer.WriteInt64(p.Age) if err != nil { return nil, err } writer.Flush() return buf.Bytes(), nil }In this example, we define a Person struct with a Name field of type string and an Age field of type int64. We then define a function called encodePerson which takes a pointer to a Person struct and returns a byte array and an error. Inside the function, we create a new MsgpWriter and write the Name field as a string using the WriteString method and write the Age field as a 64-bit integer using the WriteInt64 method. Finally, we flush the buffer and return the byte array. The package library is "github.com/tinylib/msgp".