golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.74k stars 1.58k forks source link

Message.String() #1543

Closed wslky closed 1 year ago

wslky commented 1 year ago

What did you do?

message User{
  Sex sex = 1;
}

enum Sex{
  male = 0;
  female = 1;
}
o := User{
    Sex: Sex_male,
}
fmt.Println(o.String())

What did you expect to see?

sex: male

What did you see instead?

nothing

puellanivis commented 1 year ago

Default values are not emitted by default. For enums, this is the first value which also must be = 0.

If you would like both MALE and FEMALE to be emitted, it’s recommended to use a nonsense zero value indicating it is either unknown or unset:

enum Sex{
  SEX_UNKNOWN = 0;
  MALE = 1;
  FEMALE = 2;
}