Golang IP Address Manipulation: Parse CIDR & Net IP Examples

Golang IP address parsing examples


golang ip address Golang IP address parsing is simple using the standard net package. The net package includes a variety of methods to manipulate IP addresses.

Golang IP address manipulation can be achieved by following the examples below.

Overview

Net IP Types

The net package has three main types, IP, IPMask, and IPNet. IP and IPMask represent an IP address as a slice of bytes; while IPNet represents the two together as an IP network. Typically, but not always, IPv4 is represented as 4-byte and IPv6 as a 16-byte slice.

Net IP Public Methods
Type Method
Base func ParseIP(s string) IP
func ParseCIDR(s string) (IP, *IPNet, error)
func IPv4(a, b, c, d byte) IP
func IPv4Mask(a, b, c, d byte) IPMask
func CIDRMask(ones, bits int) IPMask
IP func (ip IP) String() string
func (ip IP) MarshalText() ([]byte, error)
func (ip *IP) UnmarshalText(text []byte) error
func (ip IP) Equal(x IP) bool
func (ip IP) IsUnspecified() bool
func (ip IP) IsLoopback() bool
func (ip IP) IsMulticast() bool
func (ip IP) IsInterfaceLocalMulticast() bool
func (ip IP) IsLinkLocalMulticast() bool
func (ip IP) IsLinkLocalUnicast() bool
func (ip IP) IsGlobalUnicast() bool
func (ip IP) To4() IP
func (ip IP) To16() IP
func (ip IP) DefaultMask() IPMask
func (ip IP) Mask(mask IPMask) IP
func (ip IP) String() string
func (ip IP) MarshalText() ([]byte, error)
func (ip *IP) UnmarshalText(text []byte) error
func (ip IP) Equal(x IP) bool
IPMask func (m IPMask) Size() (ones, bits int)
func (m IPMask) String() string
IPNet func (n *IPNet) Contains(ip IP) bool
func (n *IPNet) Network() string
func (n *IPNet) String() string

Net IP Base Methods

func ParseIP(s string) IP
ParseIP takes a string IP address and returns an IP instance. It converts an IP address from a string to an IP type.

func ParseCIDR(s string) (IP, *IPNet, error)
ParseCIDR takes a CIDR IP address/mask (RFC4632 and RFC 4291) as a string (e.g., “10.0.0.1/24”) and returns the address (e.g., “10.0.0.1”) and network (e.g., “10.0.0.0/24”).

func IPv4(a, b, c, d byte) IP
IPv4 takes four byte’s and returns an IP address in 16-byte form.

func IPv4Mask(a, b, c, d byte) IPMask
IPv4Mask takes four byte’s and returns an IP mask in 4-byte form.

func CIDRMask(ones, bits int) IPMask
CIDRMask takes two ints representing ones and bits, then returns an IP mask in the form of (1 bits followed by 0s)/bits and returns ones 1 bits followed by 0s up to a total length of `bits’ bits (inverse of IPMask.Size).

Net IP IP Methods

func (ip IP) String() string
IP.String returns the IP address as a string (e.g.,”10.0.0.1″, “2001:db8::1”).

func (ip IP) MarshalText() ([]byte, error)
IP.MarshalText converts an IP type to byte slice.

func (ip *IP) UnmarshalText(text []byte) error
IP.UnmarshalText converts an byte slice to an IP type.

func (ip IP) Equal(x IP) bool
IP.Equal reports weather two IP types are the same IP address.

func (ip IP) IsUnspecified() bool
IP.IsUnspecified indicates if an IP address is not specified (e.g. IPv4 “0.0.0.0” or IPv6 “::”)

func (ip IP) IsLoopback() bool
IP.IsLoopback determines if IP is a loopback address.

func (ip IP) IsMulticast() bool
IP.IsMulticast determines if IP is a loopback address.

func (ip IP) IsInterfaceLocalMulticast() bool
IP.IsInterfaceLocalMulticast determines if IP is an interface-local multicast address.

func (ip IP) IsLinkLocalMulticast() bool
IP.IsLinkLocalMulticast determines if IP is link-local multicast address.

func (ip IP) IsLinkLocalUnicast() bool
IP.IsLinkLocalUnicast determines if IP is link-local unicast address.

func (ip IP) IsGlobalUnicast() bool
IP.IsGlobalUnicast determines if IP is global unicast address.

func (ip IP) To4() IP
IP.To4 converts an IP IPv4 address to 4-byte.

func (ip IP) To16() IP
IP.To16 converts an IP IPv4 address to 4-byte.

func (ip IP) DefaultMask() IPMask
IP.DefaultMask provides the default mask for IPv4 addresses only.

func (ip IP) Mask(mask IPMask) IP
IP.Mask provides the result of masking the IP address ip with mask

Net IP IPMask Methods

func (m IPMask) Size() (ones, bits int)
IPmask.Size provides the total leading ones and bits in the mask.

func (m IPMask) String() string
IPMask.String provides the hexadecimal form of the IPMask, with no punctuation.

Net IP IPNet Methods

func (n *IPNet) Contains(ip IP) bool
IPNet.Contains determines if the network includes the IP.

func (n *IPNet) Network() string
IPNet.Network provides the address’s network name (ip+net).

func (n *IPNet) String() string
IPNet.String provides the CIDR notation of IPNet (e.g. “10.0.0.0.24”) (RFC 4632 and RFC 4291).

Net IP Examples

ParseCIDR & IP.Mask

IP.Mask returns the result of masking the address with mask.

This example demonstrates ParseCIDR for ipv4 only. An ipv6 mask would cause it to panic.

Try it out in the Go Playground.

ParseCIDR & IPNet.Contains

ParseCIDR takes a CIDR IP address/mask (RFC4632 and RFC 4291) and returns the address and network. IPNet.Contains evaluates whether the network includes the ip.

Try it out in the Go Playground.

ParseIP, IPv4Mask, IPMask.Size, IP.Mask, IP.To4

IPv4Mask takes four byte’s and returns an IPMask. IPMask.Size provides the number of leading ones and total bits in the mask used in CIDR notation.IP.To4 converts the IPv4 address to a 4-byte representation

Try it out in the Go Playground.

IP.MarshalText & IP.UnmarshalText

Try it out in the Go Playground.

IP.IsLoopback

IP.IsLoopback, IP.To4 & IP.String

 

See also:
Golang SSH Client
Golang DNS Lookup
Golang Binary Only Package
Golang Regular Expression Match