To encode and decode data using MessagePack format in Go, you can use the msgpack
package. Here's an example of how to do it:
go get github.com/vmihailenco/msgpack
import (
"fmt"
"github.com/vmihailenco/msgpack"
)
type Person struct {
Name string
Age int
Email string
}
Marshal
function:person := Person{
Name: "John Doe",
Age: 30,
Email: "[email protected]",
}
encoded, err := msgpack.Marshal(person)
if err != nil {
fmt.Println("Error encoding data:", err)
return
}
fmt.Println("Encoded data:", encoded)
Unmarshal
function:var decoded Person
err = msgpack.Unmarshal(encoded, &decoded)
if err != nil {
fmt.Println("Error decoding data:", err)
return
}
fmt.Println("Decoded data:", decoded)
That's it! You have successfully encoded and decoded data using MessagePack format in Go.