message SearchRequest {
string query = 1;
int32 page_number = 2;
int32 result_per_page = 3;
enum Corpus {
UNIVERSAL = 0;
WEB = 1;
IMAGES = 2;
LOCAL = 3;
NEWS = 4;
PRODUCTS = 5;
VIDEO = 6;
}
Corpus corpus = 4;
}複製代碼
Corpus是一個枚舉類型,而且枚舉的第一個常量映射爲0
,每一個枚舉必須包含一個常量,0
必須是第一個元素javascript
這樣有兩個好處java
proto2
兼容你能夠爲不一樣的枚舉常量賦相同的值,可是要設置allow_alias
爲true
google
enum EnumAllowingAlias {
option allow_alias = true;
UNKNOWN = 0;
STARTED = 1;
RUNNING = 1;
}
enum EnumNotAllowingAlias {
UNKNOWN = 0;
STARTED = 1;
// RUNNING = 1; // 取消這行的註釋 會報錯
}複製代碼
varint
編碼,因此負值無效,不推薦在枚舉中定義負值更多信息請參考Google編碼