import ( "github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/protoc-gen-gogo/descriptor" "github.com/gogo/protobuf/protoc-gen-gogo/generator" ) func generate(file *descriptor.FileDescriptorProto) (*generator.ResponseFile, error) { // Parse the message definitions from the descriptor file messageMap, err := generator.NewGenerator(file).Parse() if err != nil { return nil, err } // Generate Go code for these message definitions code, err := generator.NewGenerator(file).Generate(messageMap) if err != nil { return nil, err } // Create a response file containing the generated code response := new(generator.ResponseFile) response.Name = proto.String(file.GetName() + ".pb.go") response.Content = proto.String(code) return response, nil }
import "github.com/gogo/protobuf/proto" type Person struct { Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` Age int32 `protobuf:"varint,2,opt,name=age" json:"age,omitempty"` Address string `protobuf:"bytes,3,opt,name=address" json:"address,omitempty"` } func (p *Person) Reset() { p.Name = "" p.Age = 0 p.Address = "" } func (p *Person) String() string { return proto.CompactTextString(p) } func (p *Person) ProtoMessage() {}In conclusion, the go github.com.gogo.protobuf.protoc-gen-gogo.generator package provides developers with powerful tools to generate Go code from Protocol Buffer message definitions. The FileDescriptor and Messages libraries are just a few of the many features that make this package so useful.