dongliao9018 2018-05-22 09:27
浏览 582
已采纳

如何使用Golang设置Protobuf2枚举

I'm trying to use protobuf2 enums in golang but I cannot figure it out.

I created a simple protobuf file:

syntax         =          "proto2" ;
package                    enum    ;
message Foo{
     enum Bar{
         LOL = 1;
     }
     optional Bar baz = 1;
}

And I created a simple golang file:

package main

import (
    enum "./enum"
    "github.com/golang/protobuf/proto"
)

func main() {
    msg := &enum.Foo{
        Baz: enum.Foo_LOL,
    }
    proto.Marshal(&msg)
}

I got an error.

./foo.go:10: cannot use enum.Foo_LOL (type enum.Foo_Bar) as type *enum.Foo_Bar in field value

It seemed simple enough to solve, just add a & in front of 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)
}

Nope:

./foo.go:10: cannot take the address of enum.Foo_LOL

I searched google and found this guy being trolled by a bot. He had some working code, but it was verbose enough to bore a bible scholar:

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)
}

I looked in the generated code and found an Enum method, which also worked, but was verbose enough to bore a tax auditor:

package main

import (
    enum "./enum"
    "github.com/golang/protobuf/proto"
)

func main() {
    msg := &enum.Foo{
        Baz: enum.Foo_LOL.Enum(),
    }
    proto.Marshal(msg)
}

What is the intended method?

  • 写回答

1条回答 默认 最新

  • dos49618 2018-05-22 09:37
    关注

    Protobuf with syntax="proto2" generates enum fields with pointer type, so Foo.baz will be of type *Foo_Bar. You can't assign a non-pointer Foo_Bar value to this field, only a pointer value.

    Also, the enum values you list in the protobuf files will be generated to be constants in Go. And you cannot take the address of constant values, for details see: Find address of constant in go

    If your protobuf generates an Enum() method returning a pointer to the value, then that's fine, you can use that. But this method is not always generated, so don't be surprised if you don't find this to some type / enum.

    If it's missing, the simplest is to create a (local) variable, and whose address you can take:

    lol := enum.Foo_LOL
    msg := &enum.Foo{
        Baz: &lol,
    }
    err := proto.Marshal(msg)
    

    If you have to do this many times / many places, create a helper function which fills the purpose of the Enum() method. This is how you can create one:

    func barPtr(b enum.Foo_Bar) *enum.Foo_Bar { return &b }
    

    And using it:

    msg := &enum.Foo{
        Baz: barPtr(enum.Foo_LOL),
    }
    err := proto.Marshal(msg)
    

    There are many other options to obtain a pointer to an integer type, but they are not necessarily cleaner or more efficient. You can see a list of different methods here: How do I do a literal *int64 in Go?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测