Method to turn an IPv4 into a mapped 4in6 IPv6
Fryuni opened this issue · 3 comments
There is Unmap
to turn a 4in6 IPv6 into an IPv4, but there is no way to do the other way around.
This showed up when I was developing a library that has to accept both as the same, but the only way I found to do this conversion was through the standard library with netaddr.FromStdIP(ip.IPAddr().IP.To16())
. This is horrible since it does 2 separate allocations to do what could be just an assignment.
I searched for any way to do this and the only thing I found was a mention that this wasn't yet implemented back in 2020.
The netaddr package is now deprecated. You should use net/netip
in the standard library instead, and https://pkg.go.dev/go4.org/netipx for the bits of this netaddr
package that didn't make it into net/netip
yet.
As for your question, use netaddr.IPv6Raw(ip.As16())
which doesn't allocate.
I think the new package should be mentioned on the README, I had not seen any link to it till now.