The problem of Protobuf generating go pointer

the proto file I now define is as follows

syntax = "proto2";
message Test1 {}
message Test2 {
    required  Test1 test1 = 1;
}
message Test3 {
    required Test2 test2 = 1;
}

then I will get a go file with the approximate content

type Test3 {
    Test2 *Test2 
}

type Test2 {
    Test1 *Test1
}

type Test1 {}

then when I use it, it will be very troublesome

var test3 = new(Test3)
test3.Test2 = new(Test2) // 
test3.Test2.Test1 = new(Test1) // 
fmt.Printf(test3.Test2.Test1) // 

as shown above, I have to initialize many times in order to use Test1. Because of business problems, there may be four or five levels. This is too troublesome for coding. What I can think of now is that there are two ideas

.
  1. found a way to initialize the pointer itself once and for all.
  2. find out whether protobuf has a method that does not generate pointers but only structures, or whether protobuf has a function to generate the first idea

but for these two roads are not through, please give us your advice.

Jan.11,2022

the first way is OK. Write a NewXXX method to return the object. You don't have to new, again and again like & struct {} to return



:PUBResponse RPCrespnil

message PUBResponse {

int32 code = 1;
string msg = 2;
.

}
message FindResponse {

PUBResponse resp = 1;
int64 total = 2;

}

type FindResponse struct {

Resp                 *PUBResponse `protobuf:"bytes,1,opt,name=resp,proto3" json:"resp,omitempty"`
Total                int64        `protobuf:"varint,2,opt,name=total,proto3" json:"total,omitempty"`

json: "nodes,omitempty" `

XXX_NoUnkeyedLiteral struct{}     `json:"-"`
XXX_unrecognized     []byte       `json:"-"`
XXX_sizecache        int32        `json:"-"`

}

Menu