I’ve the following ‘string’ as url: localhost:9000
I need std.Io.net.IpAddress.parseLiteral to get a IpAddress, but it can’t handle the url: error.InvalidAddress.
How to handle such a url?
I’ve the following ‘string’ as url: localhost:9000
I need std.Io.net.IpAddress.parseLiteral to get a IpAddress, but it can’t handle the url: error.InvalidAddress.
How to handle such a url?
localhost is not an IP address but a hostname. to handle this correctly you need to go through std.Io.net.Hostname.fromUri, giving it a std.Uri and then .lookup on the std.Io.net.Hostname
PS: if you know that it is always localhost, i would use ::1 or 127.0.0.1, since those don’t need the lookup step that involves io.
For localhost there is also: std.Io.net.Ip4Adress.loopback(port) / std.Io.net.Ip6Adress.loopback(port) which directly maps to 127.0.0.1 / ::1 without parsing. (and with that also no possible ParseErrors on return)
So I would use something like:
const ipv4: IpAddress = .{.ip4 = .loopback(9000)};
const ipv6: IpAddress = .{.ip6 = .loopback(9000)};
Wow, that’s not really straightforward, given all the different types needed to get something out of the chain.
So then I just need to get the port from a url, which can be localhost:port or 127.0.0.1:port. ( tokenizeScalar?)
But I’m not sure if it will be localhost, it can be any url actually.
edit:
std.Uri.Parse can’t handle ‘localhost:9000’, then uri.host is null.
In Go you can do:
conn, err := d.DialContext(ctx, "tcp", "localhost:12345")