Understanding Gos net/netip Addr Type: A Deep Dive
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.
Why choosenet/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:
- Variable
- Requires heap allocation
- May contain invalid status
- Cannot use the
==
operator for comparison
The 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!
Get startedAddr
Let’s look at the basics of creating and using Addr
:
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) }
ParseAddr
is that it is very strict. It won't accept strange formats or invalid addresses. For example:
// 这些将会失败 _, 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表示法
Discuss in depth the Addr
method
Let’s explore the key methods you’ll use with Addr
. I'll share some practical examples of where each method comes in handy.
Is this IPv4 or IPv6?
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) } }
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.
Address classification method
TheAddr
type provides several ways to classify IP addresses. Here’s a comprehensive example:
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) } } }
Practical example: Let's say you are writing a service that needs to bind to all interfaces except the loopback interface:
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 }
Use zone (IPv6 scope ID)
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:
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()) }
Practical Application: IP Address Filter
Let’s put all this together in a practical example. This is a simple IP filter that can be used for web services:
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 }
Usage example:
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)) } }
Performance Notes
One of the great things aboutnet/netip.Addr
is its performance features. Since it is a value type:
- Basic operations without heap allocation
- Efficient comparison operations
- A zero value is invalid (unlike
net.IP
, where a zero value may be valid)
Some common pitfalls and tricks
-
Don’t mix
net.IP
andnetip.Addr
randomly. While it’s possible to convert between them, for the sake of consistency, try to stick withnetip.Addr
throughout your codebase. - Note the area in the comparison In addition to the area, two identical addresses are considered different addresses.
-
Use with caution
MustParseAddr
While convenient in test or initialization code, preferParseAddr
in production code that handles user input. -
Remember it is immutable All methods that appear to modify the address (such as
WithZone
) actually return a new address.
What’s next?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...
