github.com/golang/protobuf/{proto,protoc-gen-go} . What is wrong with them? That is what is used reflection. Suppose you have a project that works with a specific set of structures. And these structures are now and again encoded in Protocol Buffers and back. If it were always different, unpredictable types, then there are no problems. But if the set is known in advance, there’s absolutely no need to use reflection. As you know, it is customary to use some interface that is responsible for encoding. Here's an example of a piece of encoding/json : type Marshaler interface { MarshalJSON() ([]byte, error) } type Unmarshaler interface { UnmarshalJSON([]byte) error } type X struct { Name string, Value int, } func (x *X) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`{"name": %q, "value": %d}`, x.Name, x.Value)) } (Un)Marshaler look so rosy. For example, here you can read about yaml ( English ) and in general on this topic. go get github.com/gogo/protobuf/{proto,protoc-gen-gogo,gogoproto,protoc-gen-gofast} protoc \ --proto_path=$GOPATH/src:$GOPATH/src/github.com/gogo/protobuf/protobuf:. \ --gogofast_out=. *.proto and you will get both extensions (if any) and acceleration. syntax="proto3"; package some; //protoc \ // --proto_path=$GOPATH/src:$GOPATH/src/github.com/gogo/protobuf/protobuf:. \ // --gogofast_out=. *.proto import "github.com/gogo/protobuf/gogoproto/gogo.proto"; // , Equal, option (gogoproto.equal_all) = true; option (gogoproto.goproto_stringer_all) = false; // Stringer ( ) option (gogoproto.stringer_all) = true; // - option (gogoproto.populate_all) = true; // option (gogoproto.testgen_all) = true; // option (gogoproto.benchgen_all) = true; // option (gogoproto.marshaler_all) = true; // option (gogoproto.sizer_all) = true; // option (gogoproto.unmarshaler_all) = true; // enums, - option (gogoproto.goproto_enum_prefix_all) = false; enum Bool { Yes = 0; No = 1; DontCare = 2; } message Some { option (gogoproto.goproto_unrecognized ) = false; option (gogoproto.goproto_getters) = false; Bool Waht = 1; int64 Count = 2; bytes Hash = 3; } /* (Size, String ..) */ type Bool int32 const ( Yes Bool = 0 No Bool = 1 DontCare Bool = 2 ) // ... type Some struct { Waht Bool `protobuf:"varint,1,opt,name=Waht,proto3,enum=some.Bool" json:"Waht,omitempty"` Count int64 `protobuf:"varint,2,opt,name=Count,proto3" json:"Count,omitempty"` Hash []byte `protobuf:"bytes,3,opt,name=Hash,proto3" json:"Hash,omitempty"` } // proto.Message (github.com/golang/protobuf/proto) func (m *Some) Reset() { *m = Some{} } func (*Some) ProtoMessage() {} // func (m *Some) Marshal() (data []byte, err error) { // ... } // func (m *Some) Unmarshal(data []byte) error { // ... } protoc --gofast_out=. myproto.proto func (m *Some) Reset() { *m = Some{} } // gogo allows you to generate "fast" structures. In this case, you can use them with the "old" github.com/golang/protobuf/proto . This will use the Marshal and Unmarshal - this is not a problem. But what if you use the same instance of a structure many times. If the structure is large (no, huge), then by and large it would not hurt to use the pool and save the “waste” structures, and then retrieve them back - reuse them.github.com/golang/protobuf/proto . Reference . func Unmarshal(buf []byte, pb Message) error { pb.Reset() // return UnmarshalMerge(buf, pb) } Reset . And consequently from *m = Some{} - the old structure is thrown out, a new one is created. This structure is small - do not care - but I would like to save Hash []byte ( I mean the allocated memory ), in case you use a big-hash.github.com/gogo/protobuf/proto similar - “copy-paste”. No glimpse.Unmarshal method directly or UnmarshalMerge - just add your MyReset method, trim the length of the slice - leave the capacity. Not! Here is a line from the generated Unmarshal : m.Hash = append([]byte{}, data[iNdEx:postIndex]...) io.Writer , reading from io.Reader - this bike already exists.json : github.com/pquerna/ffjson . Similarly for json. Not just a generator - but a Swiss knife for json + Go .net/http . Acceleration due to reuse of memory. And the same with additional features.Source: https://habr.com/ru/post/276905/
All Articles