简体   繁体   English

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

[英]What is the better way to use IP field (IPV4 or IPV6) in proto3 file for Golang and C# usage

I want to use an IP field in a proto3 message.我想在 proto3 消息中使用 IP 字段。 Which type in proto3 should I use to represent the same.我应该使用 proto3 中的哪种类型来表示相同。 The field will be used by Golang and C# implementation.该字段将由 Golang 和 C# 实现使用。 Should I use string?我应该使用字符串吗? or fixed32 for IPV4 and bytes for IPV6?还是固定 32 用于 IPV4,字节用于 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?如果是第二个,那么如何在 Golang 实现中对其进行编码? Like, should I first construct a string with a valid IP address and then convert it to fixed32 format or how is it?像,我应该首先构造一个具有有效 IP 地址的字符串,然后将其转换为 fixed32 格式还是如何?

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.几乎所有语言都支持从具有标准 ipv4 或 ipv6 符号的字符串中读取 ip 地址。 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.如果您在 fixed32 或 bytes 字段中使用二进制表示,则必须找出一种方法将其转换为您使用协议的每种语言。

You may also want to specify whether it always has to be an ip address, or if a DNS hostname could be used instead.您可能还想指定它是否始终必须是 ip 地址,或者是否可以使用 DNS 主机名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM