Avinash K R
Avinash K R

Reputation: 33

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. 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?

Upvotes: 3

Views: 1985

Answers (1)

jpa
jpa

Reputation: 12176

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.

Upvotes: 1

Related Questions