netip
packagestandard libraryThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package netip defines an IP address type that's a small value type.Building on thatAddr type, the package also definesAddrPort (anIP address and a port) andPrefix (an IP address and a bit lengthprefix).
Compared to thenet.IP type,Addr type takes less memory, is immutable,and is comparable (supports == and being a map key).
Index¶
- type Addr
- func AddrFrom16(addr [16]byte) Addr
- func AddrFrom4(addr [4]byte) Addr
- func AddrFromSlice(slice []byte) (ip Addr, ok bool)
- func IPv4Unspecified() Addr
- func IPv6LinkLocalAllNodes() Addr
- func IPv6LinkLocalAllRouters() Addr
- func IPv6Loopback() Addr
- func IPv6Unspecified() Addr
- func MustParseAddr(s string) Addr
- func ParseAddr(s string) (Addr, error)
- func (ip Addr) AppendBinary(b []byte) ([]byte, error)
- func (ip Addr) AppendText(b []byte) ([]byte, error)
- func (ip Addr) AppendTo(b []byte) []byte
- func (ip Addr) As16() (a16 [16]byte)
- func (ip Addr) As4() (a4 [4]byte)
- func (ip Addr) AsSlice() []byte
- func (ip Addr) BitLen() int
- func (ip Addr) Compare(ip2 Addr) int
- func (ip Addr) Is4() bool
- func (ip Addr) Is4In6() bool
- func (ip Addr) Is6() bool
- func (ip Addr) IsGlobalUnicast() bool
- func (ip Addr) IsInterfaceLocalMulticast() bool
- func (ip Addr) IsLinkLocalMulticast() bool
- func (ip Addr) IsLinkLocalUnicast() bool
- func (ip Addr) IsLoopback() bool
- func (ip Addr) IsMulticast() bool
- func (ip Addr) IsPrivate() bool
- func (ip Addr) IsUnspecified() bool
- func (ip Addr) IsValid() bool
- func (ip Addr) Less(ip2 Addr) bool
- func (ip Addr) MarshalBinary() ([]byte, error)
- func (ip Addr) MarshalText() ([]byte, error)
- func (ip Addr) Next() Addr
- func (ip Addr) Prefix(b int) (Prefix, error)
- func (ip Addr) Prev() Addr
- func (ip Addr) String() string
- func (ip Addr) StringExpanded() string
- func (ip Addr) Unmap() Addr
- func (ip *Addr) UnmarshalBinary(b []byte) error
- func (ip *Addr) UnmarshalText(text []byte) error
- func (ip Addr) WithZone(zone string) Addr
- func (ip Addr) Zone() string
- type AddrPort
- func (p AddrPort) Addr() Addr
- func (p AddrPort) AppendBinary(b []byte) ([]byte, error)
- func (p AddrPort) AppendText(b []byte) ([]byte, error)
- func (p AddrPort) AppendTo(b []byte) []byte
- func (p AddrPort) Compare(p2 AddrPort) int
- func (p AddrPort) IsValid() bool
- func (p AddrPort) MarshalBinary() ([]byte, error)
- func (p AddrPort) MarshalText() ([]byte, error)
- func (p AddrPort) Port() uint16
- func (p AddrPort) String() string
- func (p *AddrPort) UnmarshalBinary(b []byte) error
- func (p *AddrPort) UnmarshalText(text []byte) error
- type Prefix
- func (p Prefix) Addr() Addr
- func (p Prefix) AppendBinary(b []byte) ([]byte, error)
- func (p Prefix) AppendText(b []byte) ([]byte, error)
- func (p Prefix) AppendTo(b []byte) []byte
- func (p Prefix) Bits() int
- func (p Prefix) Contains(ip Addr) bool
- func (p Prefix) IsSingleIP() bool
- func (p Prefix) IsValid() bool
- func (p Prefix) MarshalBinary() ([]byte, error)
- func (p Prefix) MarshalText() ([]byte, error)
- func (p Prefix) Masked() Prefix
- func (p Prefix) Overlaps(o Prefix) bool
- func (p Prefix) String() string
- func (p *Prefix) UnmarshalBinary(b []byte) error
- func (p *Prefix) UnmarshalText(text []byte) error
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
This section is empty.
Types¶
typeAddr¶
type Addr struct {// contains filtered or unexported fields}
Addr represents an IPv4 or IPv6 address (with or without a scopedaddressing zone), similar tonet.IP ornet.IPAddr.
Unlikenet.IP ornet.IPAddr, Addr is a comparable valuetype (it supports == and can be a map key) and is immutable.
The zero Addr is not a valid IP address.Addr{} is distinct from both 0.0.0.0 and ::.
funcAddrFrom16¶
AddrFrom16 returns the IPv6 address given by the bytes in addr.An IPv4-mapped IPv6 address is left as an IPv6 address.(Use Unmap to convert them if needed.)
funcAddrFromSlice¶
AddrFromSlice parses the 4- or 16-byte byte slice as an IPv4 or IPv6 address.Note that anet.IP can be passed directly as the []byte argument.If slice's length is not 4 or 16, AddrFromSlice returnsAddr{}, false.
funcIPv4Unspecified¶
func IPv4Unspecified()Addr
IPv4Unspecified returns the IPv4 unspecified address "0.0.0.0".
funcIPv6LinkLocalAllNodes¶
func IPv6LinkLocalAllNodes()Addr
IPv6LinkLocalAllNodes returns the IPv6 link-local all nodes multicastaddress ff02::1.
funcIPv6LinkLocalAllRouters¶added ingo1.20
func IPv6LinkLocalAllRouters()Addr
IPv6LinkLocalAllRouters returns the IPv6 link-local all routers multicastaddress ff02::2.
funcIPv6Loopback¶added ingo1.20
func IPv6Loopback()Addr
IPv6Loopback returns the IPv6 loopback address ::1.
funcIPv6Unspecified¶
func IPv6Unspecified()Addr
IPv6Unspecified returns the IPv6 unspecified address "::".
funcMustParseAddr¶
MustParseAddr callsParseAddr(s) and panics on error.It is intended for use in tests with hard-coded strings.
funcParseAddr¶
ParseAddr parses s as an IP address, returning the result. The strings can be in dotted decimal ("192.0.2.1"), IPv6 ("2001:db8::68"),or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18").
func (Addr)AppendBinary¶added ingo1.24.0
AppendBinary implements theencoding.BinaryAppender interface.
func (Addr)AppendText¶added ingo1.24.0
AppendText implements theencoding.TextAppender interface,It is the same asAddr.AppendTo.
func (Addr)AppendTo¶
AppendTo appends a text encoding of ip,as generated byAddr.MarshalText,to b and returns the extended buffer.
func (Addr)As16¶
As16 returns the IP address in its 16-byte representation.IPv4 addresses are returned as IPv4-mapped IPv6 addresses.IPv6 addresses with zones are returned without their zone (use theAddr.Zone method to get it).The ip zero value returns all zeroes.
func (Addr)As4¶
As4 returns an IPv4 or IPv4-in-IPv6 address in its 4-byte representation.If ip is the zeroAddr or an IPv6 address, As4 panics.Note that 0.0.0.0 is not the zero Addr.
func (Addr)AsSlice¶
AsSlice returns an IPv4 or IPv6 address in its respective 4-byte or 16-byte representation.
func (Addr)BitLen¶
BitLen returns the number of bits in the IP address:128 for IPv6, 32 for IPv4, and 0 for the zeroAddr.
Note that IPv4-mapped IPv6 addresses are considered IPv6 addressesand therefore have bit length 128.
func (Addr)Compare¶
Compare returns an integer comparing two IPs.The result will be 0 if ip == ip2, -1 if ip < ip2, and +1 if ip > ip2.The definition of "less than" is the same as theAddr.Less method.
func (Addr)Is4¶
Is4 reports whether ip is an IPv4 address.
It returns false for IPv4-mapped IPv6 addresses. SeeAddr.Unmap.
func (Addr)Is4In6¶
Is4In6 reports whether ip is an "IPv4-mapped IPv6 address"as defined byRFC 4291.That is, it reports whether ip is in ::ffff:0:0/96.
func (Addr)IsGlobalUnicast¶
IsGlobalUnicast reports whether ip is a global unicast address.
It returns true for IPv6 addresses which fall outside of the currentIANA-allocated 2000::/3 global unicast space, with the exception of thelink-local address space. It also returns true even if ip is in the IPv4private address space or IPv6 unique local address space.It returns false for the zeroAddr.
func (Addr)IsInterfaceLocalMulticast¶
IsInterfaceLocalMulticast reports whether ip is an IPv6 interface-localmulticast address.
func (Addr)IsLinkLocalMulticast¶
IsLinkLocalMulticast reports whether ip is a link-local multicast address.
func (Addr)IsLinkLocalUnicast¶
IsLinkLocalUnicast reports whether ip is a link-local unicast address.
func (Addr)IsLoopback¶
IsLoopback reports whether ip is a loopback address.
func (Addr)IsMulticast¶
IsMulticast reports whether ip is a multicast address.
func (Addr)IsPrivate¶
IsPrivate reports whether ip is a private address, according toRFC 1918(IPv4 addresses) andRFC 4193 (IPv6 addresses). That is, it reports whetherip is in 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, or fc00::/7. This is thesame asnet.IP.IsPrivate.
func (Addr)IsUnspecified¶
IsUnspecified reports whether ip is an unspecified address, either the IPv4address "0.0.0.0" or the IPv6 address "::".
Note that the zeroAddr is not an unspecified address.
func (Addr)IsValid¶
IsValid reports whether theAddr is an initialized address (not the zero Addr).
Note that "0.0.0.0" and "::" are both valid values.
func (Addr)Less¶
Less reports whether ip sorts before ip2.IP addresses sort first by length, then their address.IPv6 addresses with zones sort just after the same address without a zone.
func (Addr)MarshalBinary¶
MarshalBinary implements theencoding.BinaryMarshaler interface.It returns a zero-length slice for the zeroAddr,the 4-byte form for an IPv4 address,and the 16-byte form with zone appended for an IPv6 address.
func (Addr)MarshalText¶
MarshalText implements theencoding.TextMarshaler interface,The encoding is the same as returned byAddr.String, with one exception:If ip is the zeroAddr, the encoding is the empty string.
func (Addr)Prefix¶
Prefix keeps only the top b bits of IP, producing a Prefixof the specified length.If ip is a zeroAddr, Prefix always returns a zero Prefix and a nil error.Otherwise, if bits is less than zero or greater than ip.BitLen(),Prefix returns an error.
func (Addr)String¶
String returns the string form of the IP address ip.It returns one of 5 forms:
- "invalid IP", if ip is the zeroAddr
- IPv4 dotted decimal ("192.0.2.1")
- IPv6 ("2001:db8::1")
- "::ffff:1.2.3.4" (ifAddr.Is4In6)
- IPv6 with zone ("fe80:db8::1%eth0")
Note that unlike package net's IP.String method,IPv4-mapped IPv6 addresses format with a "::ffff:"prefix before the dotted quad.
func (Addr)StringExpanded¶
StringExpanded is likeAddr.String but IPv6 addresses are expanded with leadingzeroes and no "::" compression. For example, "2001:db8::1" becomes"2001:0db8:0000:0000:0000:0000:0000:0001".
func (Addr)Unmap¶
Unmap returns ip with any IPv4-mapped IPv6 address prefix removed.
That is, if ip is an IPv6 address wrapping an IPv4 address, itreturns the wrapped IPv4 address. Otherwise it returns ip unmodified.
func (*Addr)UnmarshalBinary¶
UnmarshalBinary implements theencoding.BinaryUnmarshaler interface.It expects data in the form generated by MarshalBinary.
func (*Addr)UnmarshalText¶
UnmarshalText implements the encoding.TextUnmarshaler interface.The IP address is expected in a form accepted byParseAddr.
If text is empty, UnmarshalText sets *ip to the zeroAddr andreturns no error.
typeAddrPort¶
type AddrPort struct {// contains filtered or unexported fields}
AddrPort is an IP and a port number.
funcAddrPortFrom¶
AddrPortFrom returns anAddrPort with the provided IP and port.It does not allocate.
funcMustParseAddrPort¶
MustParseAddrPort callsParseAddrPort(s) and panics on error.It is intended for use in tests with hard-coded strings.
funcParseAddrPort¶
ParseAddrPort parses s as anAddrPort.
It doesn't do any name resolution: both the address and the portmust be numeric.
func (AddrPort)AppendBinary¶added ingo1.24.0
AppendBinary implements theencoding.BinaryAppendler interface.It returnsAddr.AppendBinary with an additional two bytes appendedcontaining the port in little-endian.
func (AddrPort)AppendText¶added ingo1.24.0
AppendText implements theencoding.TextAppender interface. Theencoding is the same as returned byAddrPort.AppendTo.
func (AddrPort)AppendTo¶
AppendTo appends a text encoding of p,as generated byAddrPort.MarshalText,to b and returns the extended buffer.
func (AddrPort)Compare¶added ingo1.22.0
Compare returns an integer comparing two AddrPorts.The result will be 0 if p == p2, -1 if p < p2, and +1 if p > p2.AddrPorts sort first by IP address, then port.
func (AddrPort)IsValid¶
IsValid reports whether p.Addr() is valid.All ports are valid, including zero.
func (AddrPort)MarshalBinary¶
MarshalBinary implements theencoding.BinaryMarshaler interface.It returnsAddr.MarshalBinary with an additional two bytes appendedcontaining the port in little-endian.
func (AddrPort)MarshalText¶
MarshalText implements theencoding.TextMarshaler interface. Theencoding is the same as returned byAddrPort.String, with one exception: ifp.Addr() is the zeroAddr, the encoding is the empty string.
func (*AddrPort)UnmarshalBinary¶
UnmarshalBinary implements theencoding.BinaryUnmarshaler interface.It expects data in the form generated byAddrPort.MarshalBinary.
func (*AddrPort)UnmarshalText¶
UnmarshalText implements the encoding.TextUnmarshalerinterface. TheAddrPort is expected in a formgenerated byAddrPort.MarshalText or accepted byParseAddrPort.
typePrefix¶
type Prefix struct {// contains filtered or unexported fields}
Prefix is an IP address prefix (CIDR) representing an IP network.
The firstPrefix.Bits() ofAddr() are specified. The remaining bits match any address.The range of Bits() is [0,32] for IPv4 or [0,128] for IPv6.
funcMustParsePrefix¶
MustParsePrefix callsParsePrefix(s) and panics on error.It is intended for use in tests with hard-coded strings.
funcParsePrefix¶
ParsePrefix parses s as an IP address prefix.The string can be in the form "192.168.1.0/24" or "2001:db8::/32",the CIDR notation defined inRFC 4632 andRFC 4291.IPv6 zones are not permitted in prefixes, and an error will be returned if azone is present.
Note that masked address bits are not zeroed. Use Masked for that.
funcPrefixFrom¶
PrefixFrom returns aPrefix with the provided IP address and bitprefix length.
It does not allocate. UnlikeAddr.Prefix,PrefixFrom does not maskoff the host bits of ip.
If bits is less than zero or greater than ip.BitLen,Prefix.Bitswill return an invalid value -1.
func (Prefix)AppendBinary¶added ingo1.24.0
AppendBinary implements theencoding.AppendMarshaler interface.It returnsAddr.AppendBinary with an additional byte appendedcontaining the prefix bits.
func (Prefix)AppendText¶added ingo1.24.0
AppendText implements theencoding.TextAppender interface.It is the same asPrefix.AppendTo.
func (Prefix)AppendTo¶
AppendTo appends a text encoding of p,as generated byPrefix.MarshalText,to b and returns the extended buffer.
func (Prefix)Contains¶
Contains reports whether the network p includes ip.
An IPv4 address will not match an IPv6 prefix.An IPv4-mapped IPv6 address will not match an IPv4 prefix.A zero-value IP will not match any prefix.If ip has an IPv6 zone, Contains returns false,because Prefixes strip zones.
func (Prefix)IsSingleIP¶
IsSingleIP reports whether p contains exactly one IP.
func (Prefix)IsValid¶
IsValid reports whether p.Bits() has a valid range for p.Addr().If p.Addr() is the zeroAddr, IsValid returns false.Note that if p is the zeroPrefix, then p.IsValid() == false.
func (Prefix)MarshalBinary¶
MarshalBinary implements theencoding.BinaryMarshaler interface.It returnsAddr.MarshalBinary with an additional byte appendedcontaining the prefix bits.
func (Prefix)MarshalText¶
MarshalText implements theencoding.TextMarshaler interface,The encoding is the same as returned byPrefix.String, with one exception:If p is the zero value, the encoding is the empty string.
func (Prefix)Masked¶
Masked returns p in its canonical form, with all but the highp.Bits() bits of p.Addr() masked off.
If p is zero or otherwise invalid, Masked returns the zeroPrefix.
func (Prefix)Overlaps¶
Overlaps reports whether p and o contain any IP addresses in common.
If p and o are of different address families or either have a zeroIP, it reports false. Like the Contains method, a prefix with anIPv4-mapped IPv6 address is still treated as an IPv6 mask.
func (*Prefix)UnmarshalBinary¶
UnmarshalBinary implements theencoding.BinaryUnmarshaler interface.It expects data in the form generated byPrefix.MarshalBinary.
func (*Prefix)UnmarshalText¶
UnmarshalText implements the encoding.TextUnmarshaler interface.The IP address is expected in a form accepted byParsePrefixor generated byPrefix.MarshalText.