我想在戈兰使用Protobuf2枚举,但我想不出来。
我创建了一个简单的protobuf文件:
syntax = "proto2" ;
package enum ;
message Foo{
enum Bar{
LOL = 1;
}
optional Bar baz = 1;
}
我创建了一个简单的golang文件:
package main
import (
enum "./enum"
"github.com/golang/protobuf/proto"
)
func main() {
msg := &enum.Foo{
Baz: enum.Foo_LOL,
}
proto.Marshal(&msg)
}
我出错了。
./foo.go:10: cannot use enum.Foo_LOL (type enum.Foo_Bar) as type *enum.Foo_Bar in field value
&
enum.Foo_Bar
package main
import (
enum "./enum"
"github.com/golang/protobuf/proto"
)
func main() {
msg := &enum.Foo{
Baz: &enum.Foo_LOL,
}
proto.Marshal(&msg)
}
不:
./foo.go:10: cannot take the address of enum.Foo_LOL
我搜索了谷歌发现 this guy being trolled by a bot . 他有一些工作代码,但它足够冗长,足以让一位圣经学者感到厌烦:
package main
import (
enum "./enum"
"github.com/golang/protobuf/proto"
)
var lolVar = enum.Foo_LOL
func main() {
msg := &enum.Foo{
Baz: &lolVar,
}
proto.Marshal(msg)
}
Enum
package main
import (
enum "./enum"
"github.com/golang/protobuf/proto"
)
func main() {
msg := &enum.Foo{
Baz: enum.Foo_LOL.Enum(),
}
proto.Marshal(msg)
}
预期的方法是什么?