Closed frederikhors closed 3 years ago
The usual recommendation is to use the GetDescription()
if the containing struct might be nil
(i.e. the Message does not appear on the wire) so typically when there is a sub-Message. So, MyWrappingProtoStruct.MyProtoStruct.GetDescription()
should always produce a value, and not panic, if MyWrappingProtoSctruct.MyProtoStruct == nil
, meanwhile MyWrappingProtoStruct.MyProtoStruct.Description
would panic under the same conditions.
If you’re just accessing flat fields of a protobuffer struct, then you can go ahead and access the values directly… that’s what the GetField()
function will be doing anyways, if the compiler can guarantee that the containing struct is definitely not nil
. (i.e. myPB := new(MyProtoStruct); fmt.Println(myPB.GetDescription(), myPB.Description)
both compile into essentially the same code, because the compiler knows that myPB
cannot be nil
so it shortcircuits the nil
guard check, and doesn’t need to generate it.)
@puellanivis Thank you very much!
I'm using this project for the first time with Go.
I'm a little bit confused:
should I use methods to get values (like
MyProtoStruct.GetDescription()
) orshould I use fields directly (like
MyProtoStruct.Description
)?