Home >Backend Development >Golang >Do enum variables in .proto files have to be uppercase?

Do enum variables in .proto files have to be uppercase?

WBOY
WBOYforward
2024-02-09 12:20:23991browse

.proto 文件中的枚举变量是否必须大写?

In .proto files, whether enumeration variables must be capitalized is a common question. According to the official documentation of Google Protocol Buffers, there is no clear requirement that the naming of enumeration variables must be capitalized. However, it is customary to use uppercase for enumeration variables to increase readability and code consistency. Additionally, if you generate code in another language, such as Java or C, it is common to convert enum variables to uppercase. In general, although there is no strict rule on whether enumeration variables in .proto files must be uppercase, it is recommended to use uppercase to comply with common programming norms and habits.

Question content

I want to create an enum in the following format, but my prototype extension throws an error, do I force the enum to be uppercase and only use underscores?

enum Language {
    en = 0;
    en-uk =1;
    en-gb =2;
    en-au =3;
    en-us =4;
    fil-en =5;
    en-in =6;
    fr =7;

}

Solution

According to the proto3 language specification, identifiers (including enumerations) must begin with a letter, and then only Contains letters, decimal digits and underscores.

ident = letter {letter | decimal places | “_”}

The following are the enumerations recommended by the Google Developer Style Guide. While style guides are not technically mandatory, in most cases you should take care to adhere to naming conventions unless you have a compelling reason to deviate from them.

Use camelcase (first letter capitalized) for enumeration type names, and use capitals_with_underscores for value names:

enum FooBar {
  FOO_BAR_UNSPECIFIED = 0;
  FOO_BAR_FIRST_VALUE = 1;
  FOO_BAR_SECOND_VALUE = 2;
}

The above is the detailed content of Do enum variables in .proto files have to be uppercase?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete