go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
You may need -f
if you specify in ~/.gitconfig
[url "ssh://git@github.com/"] insteadOf = https://github.com/
If you are not familiar with Protocl Buffers, then here ( English ) description of the 2nd version. For the third version, while there is no documentation, it remains to be content only with this . // C/C++ /* */ // 3, - syntax = "proto3"; // , go- package msg; // , message Msg { // _ = _ string key = 1; // repeated slice repeated int64 value = 2; } /* required extensions extensions ( ) Any */ // Sublime
protoc --go_out=. msg/*.proto
// package msg import proto "github.com/golang/protobuf/proto" var _ = proto.Marshal /* . , JSON */ type Msg struct { Key string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` Value []int64 `protobuf:"varint,2,rep,name=value" json:"value,omitempty"` } // , proto.Message func (m *Msg) Reset() { *m = Msg{} } func (m *Msg) String() string { return proto.CompactTextString(m) } func (*Msg) ProtoMessage() {} func init() { }
package main import ( "log" "./msg" "github.com/golang/protobuf/proto" ) func main() { // "" msg1 := &msg.Msg{ Key: "Hello Protocol Buffers", Value: []int64{1, 2, 3, 4}, } // data, err := proto.Marshal(msg1) if err != nil { log.Fatal("marshaling error: ", err) return } // ? log.Printf("data length: %d", len(data)) // msg2 := new(msg.Msg) err = proto.Unmarshal(data, msg2) if err != nil { log.Fatal("unmarshaling error: ", err) } // if msg1.Key != msg2.Key { log.Printf("unexpected value, expected '%s', got '%s'", msg1.Key, msg2.Key) } for i := 0; i < 4; i++ { if msg1.Value[i] != msg2.Value[i] { log.Printf("unexpected value, expected %d, got %d", msg1.Value[i], msg2.Value[i]) } } log.Println("Done") }
message Any { string type_url = 1; // bytes value = 2; // }
Source: https://habr.com/ru/post/252455/
All Articles