net

package
v2.6.7 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 9 Imported by: 1

Documentation

Index

Constants

View Source
const (
	IPv4Zero     = "0.0.0.0"
	IPv4Loopback = "127.0.0.1"
	IPv6Zero     = "::"
)

Variables

This section is empty.

Functions

func AssignRandPort

func AssignRandPort(ip ...string) (int, error)

AssignRandPort assigns a random port, optionally bound to ip.

ip can be an IPv4 or an IPv6 literal without brackets. An empty ip binds the system default address, which is dual-stack on most platforms.

func ExternalIP deprecated

func ExternalIP() (string, error)

ExternalIP returns the external IP address.

Deprecated: As of due v2.3.0, this function simply calls net.PublicIP.

func ExtractIP

func ExtractIP(addr net.Addr) (ip string, err error)

ExtractIP extracts the host from addr.

TCP and UDP addresses are unwrapped directly, which avoids formatting and re-splitting the address string on statements such as LocalIP and RemoteIP. Other address types fall back to splitting addr.String().

func ExtractPort

func ExtractPort(addr net.Addr) (int, error)

ExtractPort extracts the port from addr.

TCP and UDP addresses are unwrapped directly, so the port is not parsed from the address string.

func FulfillAddr

func FulfillAddr(addr string) string

FulfillAddr completes addr with the default listen host when it is missing.

An empty host means the system default, which is dual-stack on most platforms, so it is completed to the IPv6 wildcard. An explicit 0.0.0.0 keeps its IPv4-only semantics.

func InternalIP deprecated

func InternalIP() (string, error)

InternalIP returns the internal IP address.

Deprecated: As of due v2.3.0, this function simply calls net.PublicIP.

func ListenNetwork added in v2.6.7

func ListenNetwork(network, addr string) string

ListenNetwork returns the network used to listen on addr.

The IPv4 variant of network is selected when the host is an IPv4 literal, so that an explicit IPv4 address, including the IPv4 wildcard, does not create a dual-stack socket. An empty host, the IPv6 wildcard, IPv6 literals and host names keep the given network.

func ParseAddr

func ParseAddr(addr string, expose ...bool) (string, string, error)

ParseAddr parses addr into a listen address and an expose address.

The expose IP is only resolved automatically from the expose argument when addr is a wildcard address, namely 0.0.0.0:[port], [::]:[port] or :[port]. The wildcard listen host is preserved as is: 0.0.0.0 keeps an IPv4-only socket, [::] keeps an IPv6 wildcard socket and an empty host keeps the system default, which is dual-stack on most platforms.

func ParseHostPort added in v2.6.0

func ParseHostPort(endpoint string) (string, uint64, error)

ParseHostPort parses an endpoint into a host and a port.

func PrivateIP added in v2.3.0

func PrivateIP() (string, error)

PrivateIP returns the private IP address.

func PublicIP added in v2.3.0

func PublicIP() (string, error)

PublicIP returns the public IP address.

func SetPrivateIPResolver added in v2.3.3

func SetPrivateIPResolver(resolver IPResolver)

SetPrivateIPResolver sets the private IP resolver.

func SetPublicIPResolver added in v2.3.3

func SetPublicIPResolver(resolver IPResolver)

SetPublicIPResolver sets the public IP resolver.

Types

type IPResolver added in v2.3.3

type IPResolver func() (string, error)

Jump to

Keyboard shortcuts

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