分享
gogoprotobuf使用
alexstocks · · 11013 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
在go中使用google protobuf,有两个可选用的包goprotobuf(go官方出品)和gogoprotobuf。
gogoprotobuf是完全兼容google protobuf,而且经过我一些测试,它生成大代码质量确实要比goprotobuf高一些。先忙一一说明其一些选项的意义。
1 gogoproto.goproto_enum_prefix
如果选项为false,则生成的代码中不加"E_"。
enum E {
// option (gogoproto.goproto_enum_prefix) = false;
A = 0;
B = 2;
}
const (
// option (gogoproto.goproto_enum_prefix) = false;
E_A E = 0
E_B E = 2
)
or
enum E {
// option (gogoproto.goproto_enum_prefix) = false;
A = 0;
B = 2;
}
const (
A E = 0
B E = 2
)
2 gogoproto.goproto_getters
message test {
// option (gogoproto.goproto_getters) = false;
E e = 1;
}
type Test struct {
E *E `protobuf:"varint,1,opt,name=e,enum=test.E" json:"e,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *Test) GetE() E {
if m != nil && m.E != nil {
return *m.E
}
return A
}
type Test struct {
E *E `protobuf:"varint,1,opt,name=e,enum=test.E" json:"e,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信11013 次点击
上一篇:jvm.go -- 设计和实现
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
在go中使用google protobuf,有两个可选用的包goprotobuf(go官方出品)和gogoprotobuf。
gogoprotobuf是完全兼容google protobuf,而且经过我一些测试,它生成大代码质量确实要比goprotobuf高一些。先忙一一说明其一些选项的意义。
1 gogoproto.goproto_enum_prefix
如果选项为false,则生成的代码中不加"E_"。
enum E {
// option (gogoproto.goproto_enum_prefix) = false;
A = 0;
B = 2;
}
const (
// option (gogoproto.goproto_enum_prefix) = false;
E_A E = 0
E_B E = 2
)
or
enum E {
// option (gogoproto.goproto_enum_prefix) = false;
A = 0;
B = 2;
}
const (
A E = 0
B E = 2
)
2 gogoproto.goproto_getters
message test {
// option (gogoproto.goproto_getters) = false;
E e = 1;
}
type Test struct {
E *E `protobuf:"varint,1,opt,name=e,enum=test.E" json:"e,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *Test) GetE() E {
if m != nil && m.E != nil {
return *m.E
}
return A
}
type Test struct {
E *E `protobuf:"varint,1,opt,name=e,enum=test.E" json:"e,omitempty"`
XXX_unrecognized []byte `json:"-"`
}