Go language’s net/netip
package: Addr
type
Hello everyone! Today we will delve into the net/netip
package of the Go language, focusing on the Addr
type. If you've been working with Go's networking code, you may have encountered the old net.IP
type. While it has served us well, it has some drawbacks that make it less suitable for modern network code. The net/netip
package (introduced in Go 1.18) gives us a more powerful and efficient way of handling IP addresses.
net/netip.Addr
? Before we get into the details, let’s understand why this type exists. The traditional net.IP
type is basically a slice of bytes ([]byte
), which means:
==
operator for comparisonThe new Addr
type solves all these problems. It is a value type (internal structure), immutable, and always represents a valid IP address. No more defensive programming!
Addr
Let’s look at the basics of creating and using Addr
:
<code class="language-go">package main import ( "fmt" "net/netip" ) func main() { // 从字符串创建Addr addr, err := netip.ParseAddr("192.168.1.1") if err != nil { panic(err) } // 如果你绝对确定输入 addr2 := netip.MustParseAddr("2001:db8::1") fmt.Printf("IPv4: %v\nIPv6: %v\n", addr, addr2) }</code>
ParseAddr
is that it is very strict. It won't accept strange formats or invalid addresses. For example:
<code class="language-go">// 这些将会失败 _, err1 := netip.ParseAddr("256.1.2.3") // 无效的IPv4八位字节 _, err2 := netip.ParseAddr("2001:db8::1::2") // 无效的IPv6(双冒号) _, err3 := netip.ParseAddr("192.168.1.1/24") // Addr不允许CIDR表示法</code>
Addr
methodLet’s explore the key methods you’ll use with Addr
. I'll share some practical examples of where each method comes in handy.
<code class="language-go">func checkAddressType(addr netip.Addr) { if addr.Is4() { fmt.Println("这是IPv4") // 你可以在这里安全地使用As4() bytes := addr.As4() fmt.Printf("作为字节:%v\n", bytes) } else if addr.Is6() { fmt.Println("这是IPv6") // 你可以在这里安全地使用As16() bytes := addr.As16() fmt.Printf("作为字节:%v\n", bytes) } }</code>
Pro tip: When dealing with IPv4-mapped IPv6 addresses (such as ::ffff:192.0.2.1
), use Is4In6()
to detect them. This is particularly useful when writing protocol-agnostic code.
Addr
type provides several ways to classify IP addresses. Here’s a comprehensive example:
<code class="language-go">func classifyAddress(addr netip.Addr) { checks := []struct { name string fn func() bool }{ {"IsGlobalUnicast", addr.IsGlobalUnicast}, {"IsPrivate", addr.IsPrivate}, {"IsLoopback", addr.IsLoopback}, {"IsMulticast", addr.IsMulticast}, {"IsLinkLocalUnicast", addr.IsLinkLocalUnicast}, {"IsLinkLocalMulticast", addr.IsLinkLocalMulticast}, {"IsInterfaceLocalMulticast", addr.IsInterfaceLocalMulticast}, {"IsUnspecified", addr.IsUnspecified}, } for _, check := range checks { if check.fn() { fmt.Printf("地址是 %s\n", check.name) } } }</code>
Practical example: Let's say you are writing a service that needs to bind to all interfaces except the loopback interface:
<code class="language-go">func getBindableAddresses(addrs []netip.Addr) []netip.Addr { var bindable []netip.Addr for _, addr := range addrs { if !addr.IsLoopback() && !addr.IsLinkLocalUnicast() { bindable = append(bindable, addr) } } return bindable }</code>
If you are using IPv6, you will eventually run into zones. They are mainly used with link-local addresses to specify which network interface to use:
<code class="language-go">func handleZones() { // 创建一个带有区域的地址 addr := netip.MustParseAddr("fe80::1%eth0") // 获取区域 zone := addr.Zone() fmt.Printf("区域:%s\n", zone) // 比较带有区域的地址 addr1 := netip.MustParseAddr("fe80::1%eth0") addr2 := netip.MustParseAddr("fe80::1%eth1") // 由于区域不同,这些是不同的地址 fmt.Printf("相同的地址?%v\n", addr1 == addr2) // false // WithZone创建一个具有不同区域的新地址 addr3 := addr1.WithZone("eth2") fmt.Printf("新的区域:%s\n", addr3.Zone()) }</code>
Let’s put all this together in a practical example. This is a simple IP filter that can be used for web services:
<code class="language-go">type IPFilter struct { allowed []netip.Addr denied []netip.Addr } func NewIPFilter(allowed, denied []string) (*IPFilter, error) { f := &IPFilter{} // 解析允许的地址 for _, a := range allowed { addr, err := netip.ParseAddr(a) if err != nil { return nil, fmt.Errorf("无效的允许地址 %s: %w", a, err) } f.allowed = append(f.allowed, addr) } // 解析拒绝的地址 for _, d := range denied { addr, err := netip.ParseAddr(d) if err != nil { return nil, fmt.Errorf("无效的拒绝地址 %s: %w", d, err) } f.denied = append(f.denied, addr) } return f, nil } func (f *IPFilter) IsAllowed(ip string) bool { addr, err := netip.ParseAddr(ip) if err != nil { return false } // 首先检查拒绝列表 for _, denied := range f.denied { if addr == denied { return false } } // 如果没有指定允许的地址,则允许所有未被拒绝的地址 if len(f.allowed) == 0 { return true } // 检查允许列表 for _, allowed := range f.allowed { if addr == allowed { return true } } return false }</code>
Usage example:
<code class="language-go">func main() { filter, err := NewIPFilter( []string{"192.168.1.100", "10.0.0.1"}, []string{"192.168.1.50"}, ) if err != nil { panic(err) } tests := []string{ "192.168.1.100", // 允许 "192.168.1.50", // 拒绝 "192.168.1.200", // 不在任何列表中 } for _, ip := range tests { fmt.Printf("%s 允许?%v\n", ip, filter.IsAllowed(ip)) } }</code>
net/netip.Addr
is its performance features. Since it is a value type:
net.IP
, where a zero value may be valid)net.IP
and netip.Addr
randomly. While it’s possible to convert between them, for the sake of consistency, try to stick with netip.Addr
throughout your codebase. MustParseAddr
While convenient in test or initialization code, prefer ParseAddr
in production code that handles user input. WithZone
) actually return a new address. This article covers the basics and some advanced usage of the Addr
types, but there is much more to explore in the net/netip
package. In the next article, we'll look at AddrPort
, which combines an IP address with a port number - very useful for network programming.
Until then, happy coding! If you have any questions using net/netip.Addr
in your project, please feel free to contact us.
The above is the detailed content of Understanding Gos net/netip Addr Type: A Deep Dive. For more information, please follow other related articles on the PHP Chinese website!