Elevator Pitch
Go’s net.IP type was introduced 10 years ago in Go 1.0. In Go 1.18, a new type was added: netip.Addr. Why does Go need a new IP type, and what are the benefits of migrating existing code? This talk will discuss the motivations behind package netip and share lessons learned over the past 10 years.
Description
Go’s net.IP type has been a fixture of package net ever since Go 1.0, which was released in 2012. With the release of Go 1.18, a new package for working with IP addresses was added to the standard library: net/netip. Although many Go applications may not need to deal directly with IPv4 or IPv6 addresses on a daily basis, those that do can benefit from migrating to the new APIs. Some of the downsides of net.IP include:
- the underlying type of net.IP is []byte, which means it is not comparable and can’t be used with the
==
operator or as a map key - net.IPs are mutable and can be difficult to use safely when passing them between functions
- parsing IPs with net.ParseIP requires memory allocation for the underlying byte slice
- the existing APIs for parsing a valid address and determining its IP family are confusing: ParseIP, To4, and To16 all return nil for various error conditions rather than a typical Go error
When designing the netip.Addr type, each of these points were discussed in depth. The new APIs address each of these issues while also offering new advantages. netip.Addr is:
- small: only 24 bytes to represent an IPv4 or IPv6 address, with optional IPv6 zone identifier
- immutable: callers are encouraged to pass values without worrying about shared mutability concerns
- opaque: the internal representation of Addr may change in a future release of Go to enable further performance improvements
Audience members will learn how to apply these techniques in their every day Go programming, as well as how to work with IP addresses in a faster, safer, and less error-prone manner.
Notes
This talk is intended for Go developers with all levels of experience, as net/netip is a new API which provides numerous advantages over the standard library’s existing package net APIs. I will discuss the history of package net and the net.IP type, share some of the lessons learned while working heavily with IP addresses over the years, and ultimately demonstrate why it was deemed necessary to bring a new IP address package into the Go standard library.
I am a co-author of the open source package which was the prototype for net/netip (see https://inet.af/netaddr), and participated heavily in the design and implementation of that package. Learning about the implementation of net/netip is beneficial for Go programmers of all levels. For beginners, there are concrete advantages such as being able to use netip.Addr as a map key. For experts, there are interesting implementation details regarding caching of IPv6 zones and numerous tricks to appease Go’s inliner in order to generate faster and more efficient code.
I’ve been writing Go for almost a decade at this point and would love to share my knowledge of Go networking topics with a broader audience. Thank you very much for your time and for your consideration!