美文网首页
2019-08-06 go protocolbuffer

2019-08-06 go protocolbuffer

作者: cityhash123 | 来源:发表于2019-08-06 21:06 被阅读0次
    message People {
      enum PeopleName {
        Alice = 100;
        Bob = 101;
        Cathy = 102;
      }
      optional PeopleName name = 1;
    }
    

    Go protobuf生成器将枚举名称映射到值(反之亦然).您可以使用此映射将字符串转换为枚举值

    str := "Cathy"
    
    value, ok := People_PeopleName_value[str]
    if !ok {
        panic("invalid enum value")
    }
    
    var people People
    people.Name = People_PeopleName(value).Enum()
    

    相关文章

      网友评论

          本文标题:2019-08-06 go protocolbuffer

          本文链接:https://www.haomeiwen.com/subject/efrgdctx.html