国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Why choosenet/netip.Addr?
Discuss in depth the Addrmethod
Is this IPv4 or IPv6?
Address classification method
Use zone (IPv6 scope ID)
Practical Application: IP Address Filter
Performance Notes
Some common pitfalls and tricks
What’s next?
Home Backend Development Golang Understanding Go&#s net/netip Addr Type: A Deep Dive

Understanding Go&#s net/netip Addr Type: A Deep Dive

Jan 11, 2025 am 10:55 AM

Understanding Go

Detailed explanation of

Go language’s net/netip package: Addrtype

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() {
    // 從字符串創(chuàng)建Addr
    addr, err := netip.ParseAddr("192.168.1.1")
    if err != nil {
        panic(err)
    }

    // 如果你絕對(duì)確定輸入
    addr2 := netip.MustParseAddr("2001:db8::1")

    fmt.Printf("IPv4: %v\nIPv6: %v\n", addr, addr2)
}
One advantage of

ParseAddr is that it is very strict. It won't accept strange formats or invalid addresses. For example:

// 這些將會(huì)失敗
_, err1 := netip.ParseAddr("256.1.2.3")        // 無(wú)效的IPv4八位字節(jié)
_, err2 := netip.ParseAddr("2001:db8::1::2")   // 無(wú)效的IPv6(雙冒號(hào))
_, err3 := netip.ParseAddr("192.168.1.1/24")   // Addr不允許CIDR表示法

Discuss in depth the Addrmethod

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("作為字節(jié):%v\n", bytes)
    } else if addr.Is6() {
        fmt.Println("這是IPv6")
        // 你可以在這里安全地使用As16()
        bytes := addr.As16()
        fmt.Printf("作為字節(jié):%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

The

Addr 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() {
    // 創(chuàng)建一個(gè)帶有區(qū)域的地址
    addr := netip.MustParseAddr("fe80::1%eth0")

    // 獲取區(qū)域
    zone := addr.Zone()
    fmt.Printf("區(qū)域:%s\n", zone)

    // 比較帶有區(qū)域的地址
    addr1 := netip.MustParseAddr("fe80::1%eth0")
    addr2 := netip.MustParseAddr("fe80::1%eth1")

    // 由于區(qū)域不同,這些是不同的地址
    fmt.Printf("相同的地址?%v\n", addr1 == addr2)  // false

    // WithZone創(chuàng)建一個(gè)具有不同區(qū)域的新地址
    addr3 := addr1.WithZone("eth2")
    fmt.Printf("新的區(qū)域:%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("無(wú)效的允許地址 %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("無(wú)效的拒絕地址 %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
        }
    }

    // 如果沒(méi)有指定允許的地址,則允許所有未被拒絕的地址
    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 about

net/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

  1. Don’t mix 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.
  2. Note the area in the comparison In addition to the area, two identical addresses are considered different addresses.
  3. Use with caution MustParseAddr While convenient in test or initialization code, prefer ParseAddr in production code that handles user input.
  4. 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 Go&#s net/netip Addr Type: A Deep Dive. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the implications of Go's static linking by default? What are the implications of Go's static linking by default? Jun 19, 2025 am 01:08 AM

Go compiles the program into a standalone binary by default, the main reason is static linking. 1. Simpler deployment: no additional installation of dependency libraries, can be run directly across Linux distributions; 2. Larger binary size: Including all dependencies causes file size to increase, but can be optimized through building flags or compression tools; 3. Higher predictability and security: avoid risks brought about by changes in external library versions and enhance stability; 4. Limited operation flexibility: cannot hot update of shared libraries, and recompile and deployment are required to fix dependency vulnerabilities. These features make Go suitable for CLI tools, microservices and other scenarios, but trade-offs are needed in environments where storage is restricted or relies on centralized management.

How does Go ensure memory safety without manual memory management like in C? How does Go ensure memory safety without manual memory management like in C? Jun 19, 2025 am 01:11 AM

Goensuresmemorysafetywithoutmanualmanagementthroughautomaticgarbagecollection,nopointerarithmetic,safeconcurrency,andruntimechecks.First,Go’sgarbagecollectorautomaticallyreclaimsunusedmemory,preventingleaksanddanglingpointers.Second,itdisallowspointe

How do I create a buffered channel in Go? (e.g., make(chan int, 10)) How do I create a buffered channel in Go? (e.g., make(chan int, 10)) Jun 20, 2025 am 01:07 AM

To create a buffer channel in Go, just specify the capacity parameters in the make function. The buffer channel allows the sending operation to temporarily store data when there is no receiver, as long as the specified capacity is not exceeded. For example, ch:=make(chanint,10) creates a buffer channel that can store up to 10 integer values; unlike unbuffered channels, data will not be blocked immediately when sending, but the data will be temporarily stored in the buffer until it is taken away by the receiver; when using it, please note: 1. The capacity setting should be reasonable to avoid memory waste or frequent blocking; 2. The buffer needs to prevent memory problems from being accumulated indefinitely in the buffer; 3. The signal can be passed by the chanstruct{} type to save resources; common scenarios include controlling the number of concurrency, producer-consumer models and differentiation

How can you use Go for system programming tasks? How can you use Go for system programming tasks? Jun 19, 2025 am 01:10 AM

Go is ideal for system programming because it combines the performance of compiled languages ??such as C with the ease of use and security of modern languages. 1. In terms of file and directory operations, Go's os package supports creation, deletion, renaming and checking whether files and directories exist. Use os.ReadFile to read the entire file in one line of code, which is suitable for writing backup scripts or log processing tools; 2. In terms of process management, the exec.Command function of the os/exec package can execute external commands, capture output, set environment variables, redirect input and output flows, and control process life cycles, which are suitable for automation tools and deployment scripts; 3. In terms of network and concurrency, the net package supports TCP/UDP programming, DNS query and original sets.

How do I call a method on a struct instance in Go? How do I call a method on a struct instance in Go? Jun 24, 2025 pm 03:17 PM

In Go language, calling a structure method requires first defining the structure and the method that binds the receiver, and accessing it using a point number. After defining the structure Rectangle, the method can be declared through the value receiver or the pointer receiver; 1. Use the value receiver such as func(rRectangle)Area()int and directly call it through rect.Area(); 2. If you need to modify the structure, use the pointer receiver such as func(r*Rectangle)SetWidth(...), and Go will automatically handle the conversion of pointers and values; 3. When embedding the structure, the method of embedded structure will be improved, and it can be called directly through the outer structure; 4. Go does not need to force use getter/setter,

What are interfaces in Go, and how do I define them? What are interfaces in Go, and how do I define them? Jun 22, 2025 pm 03:41 PM

In Go, an interface is a type that defines behavior without specifying implementation. An interface consists of method signatures, and any type that implements these methods automatically satisfy the interface. For example, if you define a Speaker interface that contains the Speak() method, all types that implement the method can be considered Speaker. Interfaces are suitable for writing common functions, abstract implementation details, and using mock objects in testing. Defining an interface uses the interface keyword and lists method signatures, without explicitly declaring the type to implement the interface. Common use cases include logs, formatting, abstractions of different databases or services, and notification systems. For example, both Dog and Robot types can implement Speak methods and pass them to the same Anno

How do I use string functions from the strings package in Go? (e.g., len(), strings.Contains(), strings.Index(), strings.ReplaceAll()) How do I use string functions from the strings package in Go? (e.g., len(), strings.Contains(), strings.Index(), strings.ReplaceAll()) Jun 20, 2025 am 01:06 AM

In Go language, string operations are mainly implemented through strings package and built-in functions. 1.strings.Contains() is used to determine whether a string contains a substring and returns a Boolean value; 2.strings.Index() can find the location where the substring appears for the first time, and if it does not exist, it returns -1; 3.strings.ReplaceAll() can replace all matching substrings, and can also control the number of replacements through strings.Replace(); 4.len() function is used to obtain the length of the bytes of the string, but when processing Unicode, you need to pay attention to the difference between characters and bytes. These functions are often used in scenarios such as data filtering, text parsing, and string processing.

How do I use the io package to work with input and output streams in Go? How do I use the io package to work with input and output streams in Go? Jun 20, 2025 am 11:25 AM

TheGoiopackageprovidesinterfaceslikeReaderandWritertohandleI/Ooperationsuniformlyacrosssources.1.io.Reader'sReadmethodenablesreadingfromvarioussourcessuchasfilesorHTTPresponses.2.io.Writer'sWritemethodfacilitateswritingtodestinationslikestandardoutpu

See all articles