import ( "bytes" "github.com/gogo/protobuf/proto" ) type User struct { Id int32 Name string } func main() { user := &User{ Id: 123, Name: "John Doe", } buffer := new(bytes.Buffer) if err := proto.EncodeMessage(buffer, user); err != nil { // error handling } // do something with buffer.Bytes() }
import ( "bytes" "github.com/gogo/protobuf/proto" ) func main() { data := []byte{0x0a, 0x03, 0x61, 0x62, 0x63} buffer := bytes.NewBuffer(data) message, err := proto.DecodeMessage(buffer, &MyMessage{}) if err != nil { // error handling } // do something with message }This example reads a protobuf message from a byte slice and decodes it using the DecodeMessage function. The resulting message is of type MyMessage, which you need to define beforehand.