Movatterモバイル変換


[0]ホーム

URL:


netip

packagestandard library
go1.25.2Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2025 License:BSD-3-ClauseImports:9Imported by:9,146

Details

Repository

cs.opensource.google/go/go

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

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

func AddrFrom16(addr [16]byte)Addr

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.)

funcAddrFrom4

func AddrFrom4(addr [4]byte)Addr

AddrFrom4 returns the address of the IPv4 address given by the bytes in addr.

funcAddrFromSlice

func AddrFromSlice(slice []byte) (ipAddr, okbool)

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.

funcIPv6LinkLocalAllRoutersadded ingo1.20

func IPv6LinkLocalAllRouters()Addr

IPv6LinkLocalAllRouters returns the IPv6 link-local all routers multicastaddress ff02::2.

funcIPv6Loopbackadded ingo1.20

func IPv6Loopback()Addr

IPv6Loopback returns the IPv6 loopback address ::1.

funcIPv6Unspecified

func IPv6Unspecified()Addr

IPv6Unspecified returns the IPv6 unspecified address "::".

funcMustParseAddr

func MustParseAddr(sstring)Addr

MustParseAddr callsParseAddr(s) and panics on error.It is intended for use in tests with hard-coded strings.

funcParseAddr

func ParseAddr(sstring) (Addr,error)

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)AppendBinaryadded ingo1.24.0

func (ipAddr) AppendBinary(b []byte) ([]byte,error)

AppendBinary implements theencoding.BinaryAppender interface.

func (Addr)AppendTextadded ingo1.24.0

func (ipAddr) AppendText(b []byte) ([]byte,error)

AppendText implements theencoding.TextAppender interface,It is the same asAddr.AppendTo.

func (Addr)AppendTo

func (ipAddr) AppendTo(b []byte) []byte

AppendTo appends a text encoding of ip,as generated byAddr.MarshalText,to b and returns the extended buffer.

func (Addr)As16

func (ipAddr) As16() (a16 [16]byte)

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

func (ipAddr) As4() (a4 [4]byte)

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

func (ipAddr) AsSlice() []byte

AsSlice returns an IPv4 or IPv6 address in its respective 4-byte or 16-byte representation.

func (Addr)BitLen

func (ipAddr) BitLen()int

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

func (ipAddr) Compare(ip2Addr)int

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

func (ipAddr) Is4()bool

Is4 reports whether ip is an IPv4 address.

It returns false for IPv4-mapped IPv6 addresses. SeeAddr.Unmap.

func (Addr)Is4In6

func (ipAddr) Is4In6()bool

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)Is6

func (ipAddr) Is6()bool

Is6 reports whether ip is an IPv6 address, including IPv4-mappedIPv6 addresses.

func (Addr)IsGlobalUnicast

func (ipAddr) IsGlobalUnicast()bool

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.

For reference, seeRFC 1122,RFC 4291, andRFC 4632.

func (Addr)IsInterfaceLocalMulticast

func (ipAddr) IsInterfaceLocalMulticast()bool

IsInterfaceLocalMulticast reports whether ip is an IPv6 interface-localmulticast address.

func (Addr)IsLinkLocalMulticast

func (ipAddr) IsLinkLocalMulticast()bool

IsLinkLocalMulticast reports whether ip is a link-local multicast address.

func (Addr)IsLinkLocalUnicast

func (ipAddr) IsLinkLocalUnicast()bool

IsLinkLocalUnicast reports whether ip is a link-local unicast address.

func (Addr)IsLoopback

func (ipAddr) IsLoopback()bool

IsLoopback reports whether ip is a loopback address.

func (Addr)IsMulticast

func (ipAddr) IsMulticast()bool

IsMulticast reports whether ip is a multicast address.

func (Addr)IsPrivate

func (ipAddr) IsPrivate()bool

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

func (ipAddr) IsUnspecified()bool

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

func (ipAddr) IsValid()bool

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

func (ipAddr) Less(ip2Addr)bool

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

func (ipAddr) MarshalBinary() ([]byte,error)

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

func (ipAddr) MarshalText() ([]byte,error)

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)Next

func (ipAddr) Next()Addr

Next returns the address following ip.If there is none, it returns the zeroAddr.

func (Addr)Prefix

func (ipAddr) Prefix(bint) (Prefix,error)

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)Prev

func (ipAddr) Prev()Addr

Prev returns the IP before ip.If there is none, it returns the IP zero value.

func (Addr)String

func (ipAddr) String()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

func (ipAddr) StringExpanded()string

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

func (ipAddr) Unmap()Addr

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

func (ip *Addr) UnmarshalBinary(b []byte)error

UnmarshalBinary implements theencoding.BinaryUnmarshaler interface.It expects data in the form generated by MarshalBinary.

func (*Addr)UnmarshalText

func (ip *Addr) UnmarshalText(text []byte)error

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.

func (Addr)WithZone

func (ipAddr) WithZone(zonestring)Addr

WithZone returns an IP that's the same as ip but with the providedzone. If zone is empty, the zone is removed. If ip is an IPv4address, WithZone is a no-op and returns ip unchanged.

func (Addr)Zone

func (ipAddr) Zone()string

Zone returns ip's IPv6 scoped addressing zone, if any.

typeAddrPort

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

AddrPort is an IP and a port number.

funcAddrPortFrom

func AddrPortFrom(ipAddr, portuint16)AddrPort

AddrPortFrom returns anAddrPort with the provided IP and port.It does not allocate.

funcMustParseAddrPort

func MustParseAddrPort(sstring)AddrPort

MustParseAddrPort callsParseAddrPort(s) and panics on error.It is intended for use in tests with hard-coded strings.

funcParseAddrPort

func ParseAddrPort(sstring) (AddrPort,error)

ParseAddrPort parses s as anAddrPort.

It doesn't do any name resolution: both the address and the portmust be numeric.

func (AddrPort)Addr

func (pAddrPort) Addr()Addr

Addr returns p's IP address.

func (AddrPort)AppendBinaryadded ingo1.24.0

func (pAddrPort) AppendBinary(b []byte) ([]byte,error)

AppendBinary implements theencoding.BinaryAppendler interface.It returnsAddr.AppendBinary with an additional two bytes appendedcontaining the port in little-endian.

func (AddrPort)AppendTextadded ingo1.24.0

func (pAddrPort) AppendText(b []byte) ([]byte,error)

AppendText implements theencoding.TextAppender interface. Theencoding is the same as returned byAddrPort.AppendTo.

func (AddrPort)AppendTo

func (pAddrPort) AppendTo(b []byte) []byte

AppendTo appends a text encoding of p,as generated byAddrPort.MarshalText,to b and returns the extended buffer.

func (AddrPort)Compareadded ingo1.22.0

func (pAddrPort) Compare(p2AddrPort)int

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

func (pAddrPort) IsValid()bool

IsValid reports whether p.Addr() is valid.All ports are valid, including zero.

func (AddrPort)MarshalBinary

func (pAddrPort) MarshalBinary() ([]byte,error)

MarshalBinary implements theencoding.BinaryMarshaler interface.It returnsAddr.MarshalBinary with an additional two bytes appendedcontaining the port in little-endian.

func (AddrPort)MarshalText

func (pAddrPort) MarshalText() ([]byte,error)

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)Port

func (pAddrPort) Port()uint16

Port returns p's port.

func (AddrPort)String

func (pAddrPort) String()string

func (*AddrPort)UnmarshalBinary

func (p *AddrPort) UnmarshalBinary(b []byte)error

UnmarshalBinary implements theencoding.BinaryUnmarshaler interface.It expects data in the form generated byAddrPort.MarshalBinary.

func (*AddrPort)UnmarshalText

func (p *AddrPort) UnmarshalText(text []byte)error

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

func MustParsePrefix(sstring)Prefix

MustParsePrefix callsParsePrefix(s) and panics on error.It is intended for use in tests with hard-coded strings.

funcParsePrefix

func ParsePrefix(sstring) (Prefix,error)

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

func PrefixFrom(ipAddr, bitsint)Prefix

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)Addr

func (pPrefix) Addr()Addr

Addr returns p's IP address.

func (Prefix)AppendBinaryadded ingo1.24.0

func (pPrefix) AppendBinary(b []byte) ([]byte,error)

AppendBinary implements theencoding.AppendMarshaler interface.It returnsAddr.AppendBinary with an additional byte appendedcontaining the prefix bits.

func (Prefix)AppendTextadded ingo1.24.0

func (pPrefix) AppendText(b []byte) ([]byte,error)

AppendText implements theencoding.TextAppender interface.It is the same asPrefix.AppendTo.

func (Prefix)AppendTo

func (pPrefix) AppendTo(b []byte) []byte

AppendTo appends a text encoding of p,as generated byPrefix.MarshalText,to b and returns the extended buffer.

func (Prefix)Bits

func (pPrefix) Bits()int

Bits returns p's prefix length.

It reports -1 if invalid.

func (Prefix)Contains

func (pPrefix) Contains(ipAddr)bool

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

func (pPrefix) IsSingleIP()bool

IsSingleIP reports whether p contains exactly one IP.

func (Prefix)IsValid

func (pPrefix) IsValid()bool

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

func (pPrefix) MarshalBinary() ([]byte,error)

MarshalBinary implements theencoding.BinaryMarshaler interface.It returnsAddr.MarshalBinary with an additional byte appendedcontaining the prefix bits.

func (Prefix)MarshalText

func (pPrefix) MarshalText() ([]byte,error)

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

func (pPrefix) Masked()Prefix

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

func (pPrefix) Overlaps(oPrefix)bool

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)String

func (pPrefix) String()string

String returns the CIDR notation of p: "<ip>/<bits>".

func (*Prefix)UnmarshalBinary

func (p *Prefix) UnmarshalBinary(b []byte)error

UnmarshalBinary implements theencoding.BinaryUnmarshaler interface.It expects data in the form generated byPrefix.MarshalBinary.

func (*Prefix)UnmarshalText

func (p *Prefix) UnmarshalText(text []byte)error

UnmarshalText implements the encoding.TextUnmarshaler interface.The IP address is expected in a form accepted byParsePrefixor generated byPrefix.MarshalText.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp