netaddr

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2023 License: Apache-2.0 Imports: 7 Imported by: 4 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

netaddr Build Status GoDoc License

This is a Go implemenation supporting Go1.7+ of IPAddress and IPNetwork in the Python package netaddr. See godoc.

Usage

IP Address
// IPv6
ip := MustNewIPAddress("fe80::d656:43a8:fc42:948c")
fmt.Println(ip) // fe80::d656:43a8:fc42:948c

v1 := ip.Value()
fmt.Println(ip.Equal(MustNewIPAddress(v1, ip.Version()))) // true

// IPv4
ip = MustNewIPAddress("192.168.10.10")
fmt.Println(ip) // 192.168.10.10

v2 := ip.Value()
fmt.Println(ip.Equal(MustNewIPAddress(v2, ip.Version()))) // true
IP Network
net := MustNewIPNetwork("192.168.10.10/24")

fmt.Println(net.Network()) // 192.168.10.0
fmt.Println(net.Broadcast()) // 192.168.10.255
fmt.Println(net.NetworkMask()) // 255.255.255.0
fmt.Println(net.HostMask()) // 0.0.0.255
fmt.Println(net.First()) // 192.168.10.0
fmt.Println(net.Last()) // 192.168.10.255
fmt.Println(net.CIDR()) // 192.168.10.0/24
fmt.Println(net.HasStringIP("192.168.10.0")) // true
fmt.Println(net.HasStringIP("192.168.11.0")) // false
fmt.Println(net.Size()) // 16
fmt.Println(net.Previous()) // 192.168.9.0/24
fmt.Println(net.Next()) // 192.168.11.0/24
fmt.Println(net.Contains(MustNewIPNetwork("192.168.10.0/28"))) // true

Documentation

Overview

Package netaddr is a Go implementation of netaddr in Python to operate the IPv4/IPv6 address or network.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	IPv46To4      = MustNewIPNetwork("192.88.99.0/24") // 6to4 anycast relays (RFC 3068)
	IPv4Loopback  = MustNewIPNetwork("127.0.0.0/8")    // Loopback addresses (RFC 990)
	IPv4LinkLocal = MustNewIPNetwork("169.254.0.0/16") // Link-Local unicast address (RFC 3927)
	IPv4Multicast = MustNewIPNetwork("224.0.0.0/4")    // Multicast address (RFC 5771)

	IPv4Private = []IPNetwork{
		MustNewIPNetwork("10.0.0.0/8"),
		MustNewIPNetwork("100.64.0.0/10"),
		MustNewIPNetwork("172.16.0.0/12"),
		MustNewIPNetwork("192.0.0.0/24"),
		MustNewIPNetwork("192.168.0.0/16"),
		MustNewIPNetwork("198.18.0.0/15"),
		MustNewIPNetwork("239.0.0.0/8"),
	}

	IPv4Reserved = []IPNetwork{
		MustNewIPNetwork("0.0.0.0/8"),
		MustNewIPNetwork("192.0.2.0/24"),
		MustNewIPNetwork("240.0.0.0/4"),
		MustNewIPNetwork("198.51.100.0/24"),
		MustNewIPNetwork("203.0.113.0/24"),

		MustNewIPNetwork("233.252.0.0/24"),
		MustNewIPNetwork("234.0.0.0/8"),
		MustNewIPNetwork("235.0.0.0/8"),
		MustNewIPNetwork("236.0.0.0/8"),
		MustNewIPNetwork("237.0.0.0/8"),
		MustNewIPNetwork("238.0.0.0/8"),
		MustNewIPNetwork("225.0.0.0/8"),
		MustNewIPNetwork("226.0.0.0/8"),
		MustNewIPNetwork("227.0.0.0/8"),
		MustNewIPNetwork("228.0.0.0/8"),
		MustNewIPNetwork("229.0.0.0/8"),
		MustNewIPNetwork("230.0.0.0/8"),
		MustNewIPNetwork("231.0.0.0/8"),

		IPv4Loopback,
		IPv46To4,
	}
)

IPv4 Network

View Source
var (
	IPv6Loopback  = MustNewIPNetwork("::1/128")   // Loopback address
	IPv6LinkLocal = MustNewIPNetwork("fe80::/10") // Link-Local unicast address
	IPv6Multicast = MustNewIPNetwork("ff00::/8")  // Multicast address

	IPv6Private = []IPNetwork{
		MustNewIPNetwork("fc00::/7"),
		MustNewIPNetwork("fec0::/10"),
	}

	IPv6Reserved = []IPNetwork{
		MustNewIPNetwork("::/8"),
		MustNewIPNetwork("0100::/8"),
		MustNewIPNetwork("0200::/7"),
		MustNewIPNetwork("0400::/6"),
		MustNewIPNetwork("0800::/5"),
		MustNewIPNetwork("1000::/4"),
		MustNewIPNetwork("4000::/3"),
		MustNewIPNetwork("6000::/3"),
		MustNewIPNetwork("8000::/3"),
		MustNewIPNetwork("a000::/3"),
		MustNewIPNetwork("c000::/3"),
		MustNewIPNetwork("e000::/4"),
		MustNewIPNetwork("f000::/5"),
		MustNewIPNetwork("f800::/6"),
		MustNewIPNetwork("fe00::/9"),
		MustNewIPNetwork("ff00::/12"),
	}
)

IPv6 Network

Functions

This section is empty.

Types

type IPAddress

type IPAddress struct {
	// contains filtered or unexported fields
}

IPAddress represents a IPv4/IPv6 address.

Example
// IPv6
ip, err := NewIPAddress("fe80::d656:43a8:fc42:948c")
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(ip, ip.IsValid())

s := ip.Value()
fmt.Println(ip.Equal(MustNewIPAddress(s, ip.Version())))

fmt.Println(ip.IsGlobalUnicast())
fmt.Println(ip.IsInterfaceLocalMulticast())
fmt.Println(ip.IsLinkLocalMulticast())
fmt.Println(ip.IsLinkLocalUnicast())
fmt.Println(ip.IsLoopback())
fmt.Println(ip.IsMulticast())

// Ipv4
ip, err = NewIPAddress("192.168.10.10")
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(ip, ip.IsValid())
fmt.Println(ip.Bytes())

s = ip.Value()
fmt.Println(ip.Equal(MustNewIPAddress(s, ip.Version())))

fmt.Println(ip.IsGlobalUnicast())
fmt.Println(ip.IsInterfaceLocalMulticast())
fmt.Println(ip.IsLinkLocalMulticast())
fmt.Println(ip.IsLinkLocalUnicast())
fmt.Println(ip.IsLoopback())
fmt.Println(ip.IsMulticast())

// Loopback
ip, _ = NewIPAddress("127.0.0.1")
fmt.Println(ip.IsLoopback())
ip, _ = NewIPAddress("::1")
fmt.Println(ip.IsLoopback())

// Integer
ip = MustNewIPAddress(2130706433) // 127.0.0.1 => 127*256^3+1 => 2130706433
fmt.Println(ip)
Output:
fe80::d656:43a8:fc42:948c true
true
false
false
false
true
false
false
192.168.10.10 true
[192 168 10 10]
true
true
false
false
false
false
false
true
true
127.0.0.1

func MustNewIPAddress

func MustNewIPAddress(ip interface{}, version ...int) IPAddress

MustNewIPAddress is the same as NewIPAddress, but panic if an error occurs.

func NewIPAddress

func NewIPAddress(ip interface{}, version ...int) (IPAddress, error)

NewIPAddress returns a new IPAddress.

The argument, ip, is a IPv4 or IPv6 address, which maybe a string, []byte, [4]byte, [16]byte, net.IP, *big.Int, int, int64, uint, uint32 or uint64.

For *big.Int, the version is necessary, which reprensents the verion of the IP address, that's, 4 or 6.

For int, int64, uint, uint32 and uint64, the version is 4 by default.

For []byte, the version is necessary if the length of bytes is not 4 or 16.

For string, it maybe either a ipv4/ipv6 address or a integer string. If it's a integer string, it represents the integer value of the ipv4/ipv6, and must appoint the version explicitly.

func (IPAddress) Add added in v0.3.0

func (ip IPAddress) Add(num int64) IPAddress

Add returns a new IPAddress with its numerical value increased by num.

Example
fmt.Println(MustNewIPAddress("192.168.10.10").Add(10).String())
fmt.Println(MustNewIPAddress("fe80::").Add(10).String())
Output:
192.168.10.20
fe80::a

func (IPAddress) BigInt

func (ip IPAddress) BigInt() *big.Int

BigInt returns the big integer representation of the ipv4/ipv6 address.

func (IPAddress) Binary added in v0.2.0

func (ip IPAddress) Binary() string

Binary returns the binary format of the IP address.

For example, "10101010101010101010101010101010".

Example
fmt.Println(MustNewIPAddress("192.168.10.10").Binary())
fmt.Println(MustNewIPAddress("fe80::").Binary())
Output:
11000000101010000000101000001010
11111110100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

func (IPAddress) Bits added in v0.2.0

func (ip IPAddress) Bits() string

Bits returns the binary format of the IP address separated by the dot.

For example, "10101010.10101010.10101010.10101010".

Example
fmt.Println(MustNewIPAddress("192.168.10.10").Bits())
fmt.Println(MustNewIPAddress("fe80::").Bits())
Output:
11000000.10101000.00001010.00001010
11111110.10000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000.00000000

func (IPAddress) Bytes

func (ip IPAddress) Bytes() []byte

Bytes returns a []byte representation of the ip address.

func (IPAddress) Compare added in v0.4.0

func (ip IPAddress) Compare(other IPAddress) int

Compare returns an integer comparing two IPAddresses. The result will be 0 if ip==other, -1 if ip < other, and +1 if ip > other.

Example
ip1 := MustNewIPAddress("192.168.10.10")
ip2 := MustNewIPAddress("192.168.10.11")
switch ip1.Compare(ip2) {
case -1:
	fmt.Println("less")
case 0:
	fmt.Println("equal")
case 1:
	fmt.Println("greater")
}

ip1 = MustNewIPAddress("192.168.10.10")
ip2 = MustNewIPAddress("192.168.10.10")
switch ip1.Compare(ip2) {
case -1:
	fmt.Println("less")
case 0:
	fmt.Println("equal")
case 1:
	fmt.Println("greater")
}

ip1 = MustNewIPAddress("192.168.10.11")
ip2 = MustNewIPAddress("192.168.10.10")
switch ip1.Compare(ip2) {
case -1:
	fmt.Println("less")
case 0:
	fmt.Println("equal")
case 1:
	fmt.Println("greater")
}
Output:
less
equal
greater

func (IPAddress) Equal

func (ip IPAddress) Equal(other IPAddress) bool

Equal reports whether ip is equal to other.

func (IPAddress) Hex added in v0.3.0

func (ip IPAddress) Hex() string

Hex returns the hexadecimal format of the IP address.

For example, "0a0b0c0d".

Example
fmt.Println(MustNewIPAddress("10.11.12.13").Hex())
fmt.Println(MustNewIPAddress("fe80::").Hex())
Output:
0a0b0c0d
fe800000000000000000000000000000

func (IPAddress) IP

func (ip IPAddress) IP() net.IP

IP converts the ip to the type, net.IP.

func (IPAddress) IPv4

func (ip IPAddress) IPv4() IPAddress

IPv4 returns a new IPv4 IPAddress.

func (IPAddress) IPv6

func (ip IPAddress) IPv6() IPAddress

IPv6 returns a new IPv6 IPAddress.

func (IPAddress) IsGlobalUnicast

func (ip IPAddress) IsGlobalUnicast() bool

IsGlobalUnicast reports whether ip is a global unicast address.

The identification of global unicast addresses uses address type identification as defined in RFC 1122, RFC 4632 and RFC 4291 with the exception of IPv4 directed broadcast addresses. It returns true even if ip is in IPv4 private address space or local IPv6 unicast address space.

They are not

0.0.0.0  255.255.255.255  127.0.0.0/8  169.254.0.0/16  224.0.0.0/4
::  ::1  fe80::/10  ff00::/8

func (IPAddress) IsIPv4 added in v0.4.0

func (ip IPAddress) IsIPv4() bool

IsIPv4 reports whether the ip address is ipv4.

Example
fmt.Println(MustNewIPAddress("192.168.10.10").IsIPv4())
fmt.Println(MustNewIPAddress("fe80::").IsIPv4())
Output:
true
false

func (IPAddress) IsIPv6 added in v0.4.0

func (ip IPAddress) IsIPv6() bool

IsIPv6 reports whether the ip address is ipv6.

Example
fmt.Println(MustNewIPAddress("192.168.10.10").IsIPv6())
fmt.Println(MustNewIPAddress("fe80::").IsIPv6())
Output:
false
true

func (IPAddress) IsInterfaceLocalMulticast

func (ip IPAddress) IsInterfaceLocalMulticast() bool

IsInterfaceLocalMulticast reports whether ip is an interface-local multicast address. that's, ff01::/16.

func (IPAddress) IsLinkLocalMulticast

func (ip IPAddress) IsLinkLocalMulticast() bool

IsLinkLocalMulticast reports whether ip is a link-local multicast address, that's, 224.0.0.0/24 or ff02::/16.

func (IPAddress) IsLinkLocalUnicast

func (ip IPAddress) IsLinkLocalUnicast() bool

IsLinkLocalUnicast reports whether ip is a link-local unicast address, that's, 169.254.0.0/16 or fe80::/10.

func (IPAddress) IsLoopback

func (ip IPAddress) IsLoopback() bool

IsLoopback reports whether the ip address is the loopback, that's, 127.0.0.1/8 or ::1.

func (IPAddress) IsMulticast

func (ip IPAddress) IsMulticast() bool

IsMulticast reports whether the ip address is the multicast, that's, 224.0.0.0/4 or ff00::/8.

func (IPAddress) IsPrivate

func (ip IPAddress) IsPrivate() bool

IsPrivate reports whether the ip address is the private.

func (IPAddress) IsReserved

func (ip IPAddress) IsReserved() bool

IsReserved reports whether the ip address is the reserved.

func (IPAddress) IsUnicast

func (ip IPAddress) IsUnicast() bool

IsUnicast reports whether the ip address is the unicast

func (IPAddress) IsUnspecified

func (ip IPAddress) IsUnspecified() bool

IsUnspecified reports whether ip is an unspecified address, either the IPv4 address "0.0.0.0" or the IPv6 address "::".

func (IPAddress) IsValid

func (ip IPAddress) IsValid() bool

IsValid reports whether the ip address is valid.

func (IPAddress) Less

func (ip IPAddress) Less(other IPAddress) bool

Less reports whether ip is less than other.

Example
ip1 := MustNewIPAddress("192.168.10.10")
ip2 := MustNewIPAddress("192.168.10.11")
fmt.Println(ip1.Less(ip2))
Output:
true

func (IPAddress) Network

func (ip IPAddress) Network() (net IPNetwork)

Network returns the network of the ip address.

Example
ip := MustNewIPAddress("192.168.10.10")
net := ip.Network()
fmt.Println(net)

ip = MustNewIPAddress("fe80::d656:43a8:fc42:948c")
net = ip.Network()
fmt.Println(net)
Output:
192.168.10.10/32
fe80::d656:43a8:fc42:948c/128

func (IPAddress) String

func (ip IPAddress) String() string

String returns a string representation of the ip address.

It will return "" if the ip address is invalid.

Example
ip := MustNewIPAddress("192.168.10.10")
fmt.Println(ip.String())
Output:
192.168.10.10

func (IPAddress) Sub added in v0.3.0

func (ip IPAddress) Sub(num int64) IPAddress

Sub returns a new IPAddress with its numerical value decreased by num.

Example
fmt.Println(MustNewIPAddress("192.168.10.10").Sub(10).String())
fmt.Println(MustNewIPAddress("fe80::").Sub(10).String())
Output:
192.168.10.0
fe7f:ffff:ffff:ffff:ffff:ffff:ffff:fff6

func (IPAddress) Value

func (ip IPAddress) Value() string

Value returns the integer string representation of the ipv4/ipv6 address.

Example
ip1, _ := NewIPAddress("fe80::")
ip2, _ := NewIPAddress(ip1.Value(), ip1.Version())

fmt.Println(ip1.Value())
fmt.Println(ip2.Value())
fmt.Println(ip1.Equal(ip2))
fmt.Println(MustNewIPAddress("192.168.10.10").Value())
Output:
338288524927261089654018896841347694592
338288524927261089654018896841347694592
true
3232238090

func (IPAddress) Version

func (ip IPAddress) Version() int

Version returns the version of ip.

type IPNetwork

type IPNetwork struct {
	// contains filtered or unexported fields
}

IPNetwork represents a IPv4/IPv6 network.

func MustNewIPNetwork

func MustNewIPNetwork(ipnet string) IPNetwork

MustNewIPNetwork is the same as NewIPNetwork, but panic if an error occurs.

func NewIPNetwork

func NewIPNetwork(ipnet string) (IPNetwork, error)

NewIPNetwork returns a new IPNetwork.

Change 0.2.0: the subnet mask is optional. The default is 32 for IPv4, and 128 for IPv6.

func NewIPNetworkFromIP

func NewIPNetworkFromIP(ip net.IP, version int, mask int) (IPNetwork, error)

NewIPNetworkFromIP returns a new IPNetwork by net.IP.

func NewIPNetworkFromIPAddress

func NewIPNetworkFromIPAddress(ip IPAddress, mask int) (net IPNetwork, err error)

NewIPNetworkFromIPAddress returns a new IPNetwork by IPAddress.

func (IPNetwork) Address

func (net IPNetwork) Address() IPAddress

Address returns the IP address.

func (IPNetwork) Broadcast

func (net IPNetwork) Broadcast() IPAddress

Broadcast returns the broadcast address.

Example
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.Broadcast())

net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.Broadcast())
Output:
192.168.10.255
fe80::d656:43a8:fc42:948f

func (IPNetwork) CIDR

func (net IPNetwork) CIDR() IPNetwork

CIDR returns the true CIDR address for the network which omits any host bits to the right of the CIDR subnet prefix.

Example
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.CIDR())

net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.CIDR())
Output:
192.168.10.0/24
fe80::d656:43a8:fc42:9480/124

func (IPNetwork) Contains

func (net IPNetwork) Contains(other IPNetwork) bool

Contains reports whether the network contains the other network, that's, other is the subnet of the current network.

Example
net1 := MustNewIPNetwork("192.168.0.0/16")
net2 := MustNewIPNetwork("192.168.10.0/24")
net3 := MustNewIPNetwork("192.168.11.0/24")

fmt.Println(net1.Contains(net2))
fmt.Println(net2.Contains(net3))
fmt.Println(net3.Contains(net1))
Output:
true
false
false

func (IPNetwork) First

func (net IPNetwork) First() IPAddress

First returns the first ip address of the network.

Example
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.First())

net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.First())
Output:
192.168.10.0
fe80::d656:43a8:fc42:9480

func (IPNetwork) HasIP

func (net IPNetwork) HasIP(ip IPAddress) bool

HasIP reports whether the network contains this ip address.

func (IPNetwork) HasStringIP

func (net IPNetwork) HasStringIP(ip string) bool

HasStringIP reports whether the network contains this ip address.

Example
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.HasStringIP("192.168.10.0"))
fmt.Println(net.HasStringIP("192.168.11.0"))

net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.HasStringIP("fe80::d656:43a8:fc42:9481"))
fmt.Println(net.HasStringIP("fe80::d656:43a8:fc42:9400"))
Output:
true
false
true
false

func (IPNetwork) HostMask

func (net IPNetwork) HostMask() IPAddress

HostMask returns the host mask address of the network.

Example
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.HostMask())

net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.HostMask())
Output:
0.0.0.255
::f

func (IPNetwork) Last

func (net IPNetwork) Last() IPAddress

Last returns the last ip address of the network.

Example
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.Last())

net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.Last())
Output:
192.168.10.255
fe80::d656:43a8:fc42:948f

func (IPNetwork) Mask

func (net IPNetwork) Mask() int

Mask returns the size of the subnet mask.

func (IPNetwork) Network

func (net IPNetwork) Network() IPAddress

Network returns the ip address.

Example
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.Network())

net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.Network())
Output:
192.168.10.0
fe80::d656:43a8:fc42:9480

func (IPNetwork) NetworkMask

func (net IPNetwork) NetworkMask() IPAddress

NetworkMask returns the network mask address of the network.

Example
net := MustNewIPNetwork("192.168.10.10/24")
fmt.Println(net.NetworkMask())

net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Println(net.NetworkMask())
Output:
255.255.255.0
ffff:ffff:ffff:ffff:ffff:ffff:ffff:fff0

func (IPNetwork) Next

func (net IPNetwork) Next() (IPNetwork, error)

Next returns the next network of the current.

Example
// IPv4
next, _ := MustNewIPNetwork("192.168.10.10/24").Next()
fmt.Println(next)

next, _ = MustNewIPNetwork("192.168.255.10/24").Next()
fmt.Println(next)

_, err := MustNewIPNetwork("255.255.255.255/24").Next()
fmt.Println(err)

// IPv6
next, _ = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124").Next()
fmt.Println(next)

_, err = MustNewIPNetwork("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/124").Next()
fmt.Println(err)
Output:
192.168.11.0/24
192.169.0.0/24
increment exceeds address boundary
fe80::d656:43a8:fc42:9490/124
increment exceeds address boundary

func (IPNetwork) Previous

func (net IPNetwork) Previous() (IPNetwork, error)

Previous returns the previous network of the current.

Example
// IPv4
previous, _ := MustNewIPNetwork("192.168.10.10/24").Previous()
fmt.Println(previous)

previous, _ = MustNewIPNetwork("192.168.0.10/24").Previous()
fmt.Println(previous)

_, err := MustNewIPNetwork("0.0.0.0/24").Previous()
fmt.Println(err)

// IPv6
previous, _ = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124").Previous()
fmt.Println(previous)

_, err = MustNewIPNetwork("::f/124").Previous()
fmt.Println(err)
Output:
192.168.9.0/24
192.167.255.0/24
decrement is less than zero
fe80::d656:43a8:fc42:9470/124
decrement is less than zero

func (IPNetwork) Size

func (net IPNetwork) Size() float64

Size returns the number of the hosts in the network.

Example
net := MustNewIPNetwork("192.168.10.10/22")
fmt.Printf("%.0f\n", net.Size())

net = MustNewIPNetwork("fe80::/16")
fmt.Printf("%.0f\n", net.Size())

net = MustNewIPNetwork("fe80::/118")
fmt.Printf("%.0f\n", net.Size())

net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
fmt.Printf("%.0f\n", net.Size())
Output:
1024
5192296858534827628530496329220096
1024
16

func (IPNetwork) String

func (net IPNetwork) String() string

String returns a string representation of the ip network.

It will return "" if the network is invalid.

func (IPNetwork) Version

func (net IPNetwork) Version() int

Version returns the version of the network.

func (IPNetwork) Walk

func (net IPNetwork) Walk(f func(IPAddress))

Walk traverses all the hosts in the network.

Notice: For the more bigger network, please don't traverse it.

Example
net := MustNewIPNetwork("192.168.10.240/28")
net.Walk(func(ip IPAddress) {
	fmt.Println(ip)
})

net = MustNewIPNetwork("fe80::d656:43a8:fc42:948c/124")
net.Walk(func(ip IPAddress) {
	fmt.Println(ip)
})
Output:
192.168.10.240
192.168.10.241
192.168.10.242
192.168.10.243
192.168.10.244
192.168.10.245
192.168.10.246
192.168.10.247
192.168.10.248
192.168.10.249
192.168.10.250
192.168.10.251
192.168.10.252
192.168.10.253
192.168.10.254
192.168.10.255
fe80::d656:43a8:fc42:9480
fe80::d656:43a8:fc42:9481
fe80::d656:43a8:fc42:9482
fe80::d656:43a8:fc42:9483
fe80::d656:43a8:fc42:9484
fe80::d656:43a8:fc42:9485
fe80::d656:43a8:fc42:9486
fe80::d656:43a8:fc42:9487
fe80::d656:43a8:fc42:9488
fe80::d656:43a8:fc42:9489
fe80::d656:43a8:fc42:948a
fe80::d656:43a8:fc42:948b
fe80::d656:43a8:fc42:948c
fe80::d656:43a8:fc42:948d
fe80::d656:43a8:fc42:948e
fe80::d656:43a8:fc42:948f

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址