duanlvji4780
2019-08-20 12:07
浏览 134
已采纳

在proto3文件中将IP字段(IPV4或IPV6)用于Golang和C#的更好方法是什么

I want to use an IP field in a proto3 message. Which type in proto3 should I use to represent the same. The field will be used by Golang and C# implementation. Should I use string? or fixed32 for IPV4 and bytes for IPV6?

message xyz {
  string ip_addr = 1;
}

or

message xyz {
    oneof ip_addr{
       fixed32 v4 = 1;
       bytes v6 = 2;
}

If it is the second one, then how to encode that in Golang implementation? Like, should I first construct a string with a valid IP address and then convert it to fixed32 format or how is it?

图片转代码服务由CSDN问答提供 功能建议

我想在proto3消息中使用IP字段。 我应该使用proto3中的哪种类型来表示相同的类型。 该字段将由Golang和C#实现使用。 我应该使用字符串吗? 或fixed32(对于IPV4)和字节(对于IPV6)?

 消息xyz {
字符串ip_addr = 1; 
} 
   
 
 

\ n

 消息xyz {
 ip_addr中的一个{
 fixed32 v4 = 1; 
字节v6 = 2; 
} 
   
 
 

如果是第二个,那么如何在Golang实现中对其进行编码? 就像,我应该先用有效的IP地址构造一个字符串,然后将其转换为fixed32格式还是如何?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhiman2162 2019-08-20 12:15
    已采纳

    If you have no performance constraints here, I would just go with a string.

    Almost any language has support for reading an ip address from a string with standard ipv4 or ipv6 notation. If you use a binary representation in a fixed32 or bytes field, you'll have to figure out a way to convert that in each language you use your protocol with.

    You may also want to specify whether it always has to be an ip address, or if a DNS hostname could be used instead.

    点赞 评论

相关推荐 更多相似问题