Skip to content

Instantly share code, notes, and snippets.

@justdoit0823
Last active April 6, 2018 14:45
Show Gist options
  • Save justdoit0823/37929551e011ba1f73159a91b5ea5897 to your computer and use it in GitHub Desktop.
Save justdoit0823/37929551e011ba1f73159a91b5ea5897 to your computer and use it in GitHub Desktop.
How oneof type works in protocol buffers.

If you have a message with many fields and where at most one field will be set at the same time, you can enforce this behavior and save memory by using the oneof feature.

Oneof fields are like regular fields except all the fields in a oneof share memory, and at most one field can be set at the same time. Setting any member of the oneof automatically clears all the other members. You can check which value in a oneof is set (if any) using a special case() or WhichOneof() method, depending on your chosen language.

This type may have no any fields, and can't have exact one field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment