message := &pb.MyMessage{Name: "Alice", Age: 27} buf, err := proto.Marshal(message) if err != nil { log.Fatalf("Failed to marshal message: %v", err) } grpc.EncodeProto(buf) // Encode message using protobuf codec
var buf []byte = []byte(`{"Name": "Alice", "Age": 27}`) message := &pb.MyMessage{} err := grpc.DecodeJSON(buf, message) // Decode message using JSON codec if err != nil { log.Fatalf("Failed to decode message: %v", err) } fmt.Printf("Name: %v, Age: %v", message.Name, message.Age)In both examples, the grpc package is used to call the EncodeProto and DecodeJSON functions to encode and decode messages, respectively. The package being used is google.golang.org/grpc.