net is the foundation every network-facing Go program builds on: dialing and listening for TCP, UDP, and Unix sockets, parsing and inspecting IP addresses, and resolving hostnames. net/http, net/rpc, and this site's own TCP server and WebSocket guides all sit on top of it.
Examples
net.Dial takes a network ("tcp", "udp", "unix") and an address, returning a Conn once the connection succeeds.
conn, err := net.Dial("tcp", "example.com:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()Dial blocks until the connection is established. The returned Conn is a plain io.ReadWriteCloser, the same interface a file or a bytes.Buffer could satisfy.
conn, err := net.Dial("tcp", "example.com:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
fmt.Fprint(conn, "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")
status, err := bufio.NewReader(conn).ReadString('\n')
fmt.Print(status)HTTP/1.1 200 OKA listener hands back one Conn per Accept call. Handling each connection in its own goroutine is the standard shape for a concurrent server.
ln, err := net.Listen("tcp", ":9000")
if err != nil {
log.Fatal(err)
}
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
continue
}
go func(c net.Conn) {
defer c.Close()
io.Copy(c, c) // echo whatever the client sends
}(conn)
}# blocks, serving connections until the process exitsResolving up front, instead of passing a bare string to Dial or Listen, is how a program validates an address before acting on it.
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:9000")
if err != nil {
log.Fatal(err)
}
fmt.Println(addr.IP, addr.Port)127.0.0.1 9000SplitHostPort and JoinHostPort handle the bracket syntax IPv6 addresses need in a "host:port" string, so hand-rolled string.Split on ":" breaks on them silently.
host, port, err := net.SplitHostPort("localhost:8080")
fmt.Println(host, port, err)
fmt.Println(net.JoinHostPort("localhost", "8080"))localhost 8080
localhost:8080 Dialing and listening
| Function | Description |
|---|---|
Dial(network, address string) (Conn, error) | Connects, blocking until the connection is up. |
Three forms, and they differ only in what can stop them. The plain call waits as long as the OS does, which is minutes. | |
DialTimeout(network, address string, timeout time.Duration) (Conn, error) net.DialTimeout("tcp", addr, 5*time.Second) | Dial with a ceiling on how long it may take. |
| |
Listen(network, address string) (Listener, error) | Opens a listening socket for a stream network. |
Accept in a loop, and hand each connection to its own goroutine. The error branch is the part worth getting right: closing the listener makes the blocked | |
ListenPacket(network, address string) (PacketConn, error) pc, err := net.ListenPacket("udp", ":9001") | Opens a listening socket for a packet network. |
| |
Pipe() (Conn, Conn) client, server := net.Pipe() | Two connected in-memory Conns. |
| |
FileConn(f *os.File) (Conn, error) conn, err := net.FileConn(f) | Wraps an inherited descriptor as a Conn. |
| |
FileListener(f *os.File) (Listener, error) ln, err := net.FileListener(f) | The same, for a listening socket. |
| |
FilePacketConn(f *os.File) (PacketConn, error) | The same, for a packet socket. |
| |
Typed dialing and listening
| Function | Description |
|---|---|
ResolveTCPAddr(network, address string) (*TCPAddr, error) addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:9000") | Parses and resolves a TCP host:port. |
| |
DialTCP(network string, laddr, raddr *TCPAddr) (*TCPConn, error) c, err := net.DialTCP("tcp", nil, raddr) | Dial, typed to TCP. |
| |
ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error) ln, err := net.ListenTCP("tcp", laddr) | Listen, typed to TCP. |
| |
ResolveUDPAddr(network, address string) (*UDPAddr, error) addr, err := net.ResolveUDPAddr("udp", "239.0.0.1:9999") | Parses and resolves a UDP host:port. |
| |
DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error) c, err := net.DialUDP("udp", nil, raddr) | A UDP socket with one fixed peer. |
| |
ListenUDP(network string, laddr *UDPAddr) (*UDPConn, error) c, err := net.ListenUDP("udp", laddr) | A UDP socket that receives from anyone. |
| |
ListenMulticastUDP(network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) c, err := net.ListenMulticastUDP("udp4", ifi, gaddr) | Joins a multicast group on one interface. |
| |
ResolveUnixAddr(network, address string) (*UnixAddr, error) addr, err := net.ResolveUnixAddr("unix", "/tmp/app.sock") | Parses a Unix socket path. |
| |
DialUnix(network string, laddr, raddr *UnixAddr) (*UnixConn, error) c, err := net.DialUnix("unix", nil, raddr) | Connects to a Unix domain socket. |
| |
ListenUnix(network string, laddr *UnixAddr) (*UnixListener, error) ln, err := net.ListenUnix("unix", laddr) | Listens on a Unix socket path. |
| |
ListenUnixgram(network string, laddr *UnixAddr) (*UnixConn, error) | The packet-oriented Unix socket. |
| |
ResolveIPAddr(network, address string) (*IPAddr, error) addr, err := net.ResolveIPAddr("ip", "example.com") | Resolves a bare host, with no port. |
| |
Addresses, networks, and interfaces
| Function | Description |
|---|---|
ParseIP(s string) IP net.ParseIP("192.168.1.10") | Parses an address. Returns nil on bad input. |
| |
IPv4(a, b, c, d byte) IP net.IPv4(127, 0, 0, 1) | Builds an IPv4 address from four bytes. |
| |
ParseCIDR(s string) (IP, *IPNet, error) | Splits CIDR notation into an address and its network. |
Two values come back and they are not the same thing. The first is the address exactly as written, host bits and all. The second is the network with those bits cleared, and it is the one to keep when the question is whether some other address falls inside the range. | |
CIDRMask(ones, bits int) IPMask net.CIDRMask(24, 32) // 255.255.255.0 | Builds a mask of ones leading 1-bits. |
| |
ParseMAC(s string) (HardwareAddr, error) hw, err := net.ParseMAC("00:1b:44:11:3a:b7") | Parses a hardware address. |
| |
JoinHostPort(host, port string) string net.JoinHostPort("localhost", "8080") | Builds "host:port", bracketing an IPv6 host. |
| |
SplitHostPort(hostport string) (host, port string, err error) host, port, err := net.SplitHostPort("[::1]:8080") | The inverse, and it understands brackets. |
| |
Interfaces() ([]Interface, error) ifis, err := net.Interfaces() | Lists the machine's network interfaces. |
| |
InterfaceByName(name string) (*Interface, error) ifi, err := net.InterfaceByName("eth0") | One interface, by name. |
| |
InterfaceByIndex(index int) (*Interface, error) | One interface, by index. |
| |
InterfaceAddrs() ([]Addr, error) addrs, err := net.InterfaceAddrs() | Every address on every interface. |
| |
DNS lookups
| Function | Description |
|---|---|
LookupHost(host string) ([]string, error) addrs, err := net.LookupHost("go.dev") | Hostname to addresses, as strings. |
| |
LookupIP(host string) ([]IP, error) ips, err := net.LookupIP("go.dev") | The same, returning parsed addresses. |
| |
LookupAddr(addr string) ([]string, error) names, err := net.LookupAddr("8.8.8.8") | Reverse DNS: address to hostnames. |
| |
LookupPort(network, service string) (int, error) port, err := net.LookupPort("tcp", "https") | Service name to port number. |
| |
LookupCNAME(host string) (string, error) | The canonical name a host resolves through. |
| |
LookupMX(name string) ([]*MX, error) mxs, err := net.LookupMX("gmail.com") | Mail exchanger records, sorted by preference. |
| |
LookupNS(name string) ([]*NS, error) | The name servers for a domain. |
| |
LookupTXT(name string) ([]string, error) txts, err := net.LookupTXT("go.dev") | TXT records, one string per record. |
| |
LookupSRV(service, proto, name string) (string, []*SRV, error) cname, srvs, err := net.LookupSRV("xmpp", "tcp", "example.com") | Service records, for discovery. |
| |
Core interfaces
Open a row for the interface's method set and what satisfies it.
| Type | Description |
|---|---|
type Conn interface8 methods | A stream connection. This is what Dial hands back. |
Read(b []byte) (n int, err error) Blocks until bytes arrive, the deadline passes, or the peer closes. Write(b []byte) (n int, err error) Writes all of b, or returns an error. A write can block once the peer stops reading. Close() error Closes both directions. Safe to call from another goroutine, which is how you unblock a Read. LocalAddr() Addr This end of the connection. RemoteAddr() Addr The other end. What a server logs to identify a client. SetDeadline(t time.Time) error An absolute time, not a duration, and it covers reads and writes both. Push it forward after each successful operation to get an idle timeout. SetReadDeadline(t time.Time) error The read half only. A zero time.Time clears it. SetWriteDeadline(t time.Time) error The write half only. Implemented by *TCPConn, *UDPConn, *UnixConn, *IPConn, *tls.Conn, and the pair from net.Pipe Read and write like a file, with one addition that matters: a deadline. Nothing else bounds a read, so a peer that connects and then goes quiet holds the goroutine open forever. | |
type Listener interface3 methods | A listening socket. One Accept, one connection. |
Accept() (Conn, error) Blocks until a client connects. After Close it returns an error wrapping net.ErrClosed, which is how the loop knows to stop rather than spin. Close() error Stops accepting. Connections already handed out stay open. Addr() Addr The address actually bound, which is how you read back the port after listening on :0. Implemented by *TCPListener, *UnixListener, and the listener tls.NewListener wraps Accept in a loop, one goroutine per connection. Closing the listener is what makes the blocked | |
type PacketConn interface7 methods | A packet socket. No peer, so every read reports a sender. |
ReadFrom(p []byte) (n int, addr Addr, err error) One packet, and who sent it. WriteTo(p []byte, addr Addr) (n int, err error) One packet, to one address. Close() error Closes the socket. LocalAddr() Addr The address this socket is bound to. SetDeadline(t time.Time) error Covers reads and writes. SetReadDeadline(t time.Time) error The read half only. SetWriteDeadline(t time.Time) error The write half only. Implemented by *UDPConn, *UnixConn on unixgram, *IPConn One call moves one datagram, and the sender comes back with it because there is no fixed peer to assume. Size the buffer for the largest packet you expect; anything longer is truncated and the rest is gone. | |
type Addr interface2 methods | An address on some network. |
Network() string The network name, "tcp" or "udp". String() string The address in text, "192.0.2.1:25" or "[2001:db8::1]:80". Implemented by *TCPAddr, *UDPAddr, *UnixAddr, *IPAddr, *IPNet Two strings is all the interface promises, so reaching the port means asserting to the concrete type. That is cheaper and safer than parsing the string back apart. | |
type Error interface2 methods | What every error in this package satisfies. |
Timeout() bool Whether the failure was a deadline or timeout expiring. A retry makes sense here and usually does not otherwise. Temporary() bool Deprecated, and never meant much. Do not branch on it. Implemented by *OpError, *DNSError, *AddrError, and the string error types
| |
Types
Open a row for the type's full method set.
| Type | Description |
|---|---|
type TCPConn struct22 methods | A TCP connection, with the TCP-only knobs. |
Read(b []byte) (int, error) The Conn read. Write(b []byte) (int, error) The Conn write. ReadFrom(r io.Reader) (int64, error) Pulls from r straight into the socket. io.Copy finds this and can reach the kernel's sendfile path. WriteTo(w io.Writer) (int64, error) Drains the connection into w. Close() error Closes both directions. CloseRead() error Half-close. Stops reading, leaves writing open. CloseWrite() error Half-close the other way, which sends the peer an EOF while you keep reading their reply. This is how a protocol says "that is all I am sending". LocalAddr() Addr This end. RemoteAddr() Addr The other end. SetDeadline(t time.Time) error Reads and writes. SetReadDeadline(t time.Time) error Reads only. SetWriteDeadline(t time.Time) error Writes only. SetNoDelay(noDelay bool) error True turns Nagle's algorithm off, so a small write goes out immediately instead of waiting to be coalesced. On by default, and what you want for a request-response protocol. SetKeepAlive(keepalive bool) error Turns on the OS keepalive probes that notice a peer that vanished without closing. SetKeepAlivePeriod(d time.Duration) error How long idle before the first probe. SetKeepAliveConfig(config KeepAliveConfig) error Idle time, probe interval, and probe count in one call. SetLinger(sec int) error What Close does with unsent data. Zero discards it and sends a reset; negative is the default, which is to keep trying in the background. SetReadBuffer(bytes int) error The kernel's receive buffer size. SetWriteBuffer(bytes int) error The kernel's send buffer size. MultipathTCP() (bool, error) Whether this connection actually negotiated multipath TCP. File() (f *os.File, err error) A duplicate of the descriptor. The connection stays open and the file is yours to close. SyscallConn() (syscall.RawConn, error) The raw connection, for a socket option Go does not wrap. Satisfies net.Conn, io.Reader, io.Writer, io.Closer, io.ReaderFrom, io.WriterTo The typed connection is what gets you the TCP-only calls. | |
type TCPListener struct7 methods | A TCP listener, returning typed connections. |
Accept() (Conn, error) The interface method, returning the connection as a Conn. AcceptTCP() (*TCPConn, error) The same connection, typed, so the TCP-only methods are there without an assertion. Close() error Stops accepting. Addr() Addr The bound address. SetDeadline(t time.Time) error Makes a blocked Accept give up at t, which is one way to run a shutdown check on a timer. File() (f *os.File, err error) A duplicate of the descriptor, for handing the socket to a child process. SyscallConn() (syscall.RawConn, error) The raw connection. Satisfies net.Listener
| |
type UDPConn struct22 methods | A UDP socket, connected or not. |
Read(b []byte) (int, error) One packet, on a connected socket. Write(b []byte) (int, error) One packet, on a connected socket. ReadFrom(b []byte) (int, Addr, error) The PacketConn read: one packet and its sender. ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) The same, with the sender typed, so the port is a field rather than a parse. ReadFromUDPAddrPort(b []byte) (n int, addr netip.AddrPort, err error) The same again as a netip.AddrPort, which is comparable and allocation-free. The one to reach for in a hot loop. WriteTo(b []byte, addr Addr) (int, error) The PacketConn write. WriteToUDP(b []byte, addr *UDPAddr) (int, error) The same, typed. WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error) The netip form. ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) Packet plus out-of-band control data, which is where the destination address of a multicast packet lives. ReadMsgUDPAddrPort(b, oob []byte) (n, oobn, flags int, addr netip.AddrPort, err error) The netip form. WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) Packet plus control data, outbound. WriteMsgUDPAddrPort(b, oob []byte, addr netip.AddrPort) (n, oobn int, err error) The netip form. Close() error Closes the socket. LocalAddr() Addr The bound address. RemoteAddr() Addr The fixed peer, or nil on a listening socket. SetDeadline(t time.Time) error Reads and writes. SetReadDeadline(t time.Time) error Reads only. SetWriteDeadline(t time.Time) error Writes only. SetReadBuffer(bytes int) error The kernel receive buffer. Worth raising on a socket taking bursts, since an overflowed buffer drops packets silently. SetWriteBuffer(bytes int) error The kernel send buffer. File() (f *os.File, err error) A duplicate of the descriptor. SyscallConn() (syscall.RawConn, error) The raw connection. Satisfies net.Conn, net.PacketConn Reading with the | |
type UnixConn struct20 methods | A Unix domain socket, stream or packet. |
Read(b []byte) (int, error) The Conn read. Write(b []byte) (int, error) The Conn write. ReadFrom(b []byte) (int, Addr, error) The packet read, for a unixgram socket. ReadFromUnix(b []byte) (int, *UnixAddr, error) The same, typed. WriteTo(b []byte, addr Addr) (int, error) The packet write. WriteToUnix(b []byte, addr *UnixAddr) (int, error) The same, typed. ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) Data plus control data. This is how a file descriptor gets passed between processes. WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error) The other half of descriptor passing. CloseRead() error Half-close the read side. CloseWrite() error Half-close the write side. Close() error Closes the socket. LocalAddr() Addr This end. RemoteAddr() Addr The other end. SetDeadline(t time.Time) error Reads and writes. SetReadDeadline(t time.Time) error Reads only. SetWriteDeadline(t time.Time) error Writes only. SetReadBuffer(bytes int) error The kernel receive buffer. SetWriteBuffer(bytes int) error The kernel send buffer. File() (f *os.File, err error) A duplicate of the descriptor. SyscallConn() (syscall.RawConn, error) The raw connection. Satisfies net.Conn, net.PacketConn A local socket, so the same reads and writes as TCP with none of the network in between. | |
type UnixListener struct8 methods | A listener on a Unix socket path. |
Accept() (Conn, error) The interface method. AcceptUnix() (*UnixConn, error) The typed form. Close() error Stops accepting, and removes the socket file unless SetUnlinkOnClose said otherwise. Addr() Addr The bound path. SetDeadline(t time.Time) error Makes a blocked Accept give up at t. SetUnlinkOnClose(unlink bool) Whether Close deletes the socket file. False is what you want when the path was handed to you, such as a systemd socket. File() (f *os.File, err error) A duplicate of the descriptor. SyscallConn() (syscall.RawConn, error) The raw connection. Satisfies net.Listener The socket is a file on disk, and | |
type IP []byte17 methods | An address, 4 or 16 bytes of it. |
String() string The text form. An invalid address prints as "<nil>". Equal(x IP) bool Compares by value, treating the 4-byte and 16-byte forms of one IPv4 address as equal. The only correct way to compare two IPs. To4() IP The 4-byte form, or nil when the address is not IPv4. To16() IP The 16-byte form. Never nil for a valid address. Mask(mask IPMask) IP The network part, with the host bits cleared. DefaultMask() IPMask The old class-based mask for an IPv4 address. Classful routing is history; take the mask from ParseCIDR instead. IsLoopback() bool 127.0.0.0/8 or ::1. IsPrivate() bool The RFC 1918 ranges, and fc00::/7. What to check before trusting a client-supplied address. IsMulticast() bool A multicast address. IsUnspecified() bool 0.0.0.0 or ::, which is what listening on every interface reports. IsGlobalUnicast() bool A routable address, excluding loopback, link-local, and multicast. IsLinkLocalUnicast() bool 169.254.0.0/16 or fe80::/10. IsLinkLocalMulticast() bool Link-local multicast, such as mDNS. IsInterfaceLocalMulticast() bool IPv6 interface-local multicast. MarshalText() ([]byte, error) The text form, so an IP in a struct encodes as a string in JSON. AppendText(b []byte) ([]byte, error) The same, appended to b instead of allocating. UnmarshalText(text []byte) error Parses the text form back. Satisfies net.Addr through IPAddr, fmt.Stringer, encoding.TextMarshaler A bad address comes back as nil rather than an error, so the check is a nil test. Use | |
type IPNet struct3 methods | A network: a base address and a mask. |
Contains(ip IP) bool Whether an address falls in the network. This is the allowlist check. Network() string Always "ip+net". String() string CIDR notation, "10.0.0.0/24". Satisfies net.Addr, fmt.Stringer
| |
type IPMask []byte2 methods | A bitmask the same width as an address. |
Size() (ones, bits int) The prefix length and the total width, so a mask turns back into the /24 a human reads. Both are zero for a mask that is not a run of leading ones. String() string The mask in hex. Satisfies fmt.Stringer Masking an address clears its host bits, which is how you get from an address to the network it sits in. | |
type TCPAddr struct3 methods | A resolved TCP endpoint. IP, Port, Zone. |
Network() string Always "tcp". String() string "host:port", with an IPv6 host bracketed. AddrPort() netip.AddrPort The netip equivalent, which is comparable and usable as a map key. Satisfies net.Addr The fields are there, so reading a client's port is a field access rather than a parse. | |
type UDPAddr struct3 methods | A resolved UDP endpoint. IP, Port, Zone. |
Network() string Always "udp". String() string "host:port". AddrPort() netip.AddrPort The comparable form, which is what you want as the key of a per-peer map. Satisfies net.Addr What | |
type UnixAddr struct2 methods | A socket path. Name and Net. |
Network() string "unix", "unixgram", or "unixpacket". String() string The filesystem path. Satisfies net.Addr A path rather than a host and port, so | |
type IPAddr struct2 methods | An address with no port. IP and Zone. |
Network() string Always "ip". String() string The address, plus a zone after a percent sign when it has one. Satisfies net.Addr An address with no port on it, which is what a bare hostname resolves to. The | |
type HardwareAddr []byte1 method | A MAC address. |
String() string Colon-separated hex, "00:1b:44:11:3a:b7". Satisfies fmt.Stringer Six bytes for Ethernet, and a slice rather than an array so the longer formats fit too. Printing it gives the colon-separated hex back. | |
type Dialer struct8 methods | A dialer you configure, and the one to use. |
DialContext(ctx context.Context, network, address string) (Conn, error) Dials, giving up when ctx is done. The one to reach for, since a handler's ctx already carries the client's disconnect. Dial(network, address string) (Conn, error) The same without a context, bounded only by the Dialer's own Timeout. DialTCP(ctx context.Context, network string, laddr, raddr netip.AddrPort) (*TCPConn, error) Typed to TCP, taking netip addresses. DialUDP(ctx context.Context, network string, laddr, raddr netip.AddrPort) (*UDPConn, error) Typed to UDP. DialUnix(ctx context.Context, network string, laddr, raddr *UnixAddr) (*UnixConn, error) Typed to Unix sockets. DialIP(ctx context.Context, network string, laddr, raddr netip.Addr) (*IPConn, error) A raw IP socket, which usually needs privileges. MultipathTCP() bool Whether this dialer will try multipath TCP. SetMultipathTCP(use bool) Turns that on or off. Declare one and keep it. | |
type ListenConfig struct4 methods | The listening side of Dialer. |
Listen(ctx context.Context, network, address string) (Listener, error) Opens a stream listener. The context covers the setup, not the lifetime of the listener. ListenPacket(ctx context.Context, network, address string) (PacketConn, error) Opens a packet socket. MultipathTCP() bool Whether this config will accept multipath TCP. SetMultipathTCP(use bool) Turns that on or off. The listening half of | |
type Resolver struct11 methods | The Lookup functions, with a context and a choice of resolver. |
LookupHost(ctx context.Context, host string) (addrs []string, err error) Addresses as strings. LookupIP(ctx context.Context, network, host string) ([]IP, error) Parsed addresses, filtered by network: "ip", "ip4", or "ip6". LookupIPAddr(ctx context.Context, host string) ([]IPAddr, error) Addresses with their IPv6 zones attached. LookupNetIP(ctx context.Context, network, host string) ([]netip.Addr, error) The netip form, comparable and allocation-free. LookupAddr(ctx context.Context, addr string) ([]string, error) Reverse lookup. LookupPort(ctx context.Context, network, service string) (port int, err error) Service name to port. LookupCNAME(ctx context.Context, host string) (string, error) The canonical name. LookupMX(ctx context.Context, name string) ([]*MX, error) Mail exchangers, sorted by preference. LookupNS(ctx context.Context, name string) ([]*NS, error) Name servers. LookupTXT(ctx context.Context, name string) ([]string, error) TXT records. LookupSRV(ctx context.Context, service, proto, name string) (string, []*SRV, error) Service records, for discovery. Every method takes a context, which the package-level | |
type Interface struct2 methods | One network interface. Index, MTU, Name, HardwareAddr, Flags. |
Addrs() ([]Addr, error) The addresses assigned to this interface, each an *IPNet. MulticastAddrs() ([]Addr, error) The multicast groups it has joined. Finding the machine's own routable address means filtering: skip the ones that are down and skip loopback. | |
type OpError struct4 methods | What almost every error from this package really is. |
Error() string The fields joined: "dial tcp 10.0.0.1:80: connect: connection refused". Unwrap() error The underlying error. What errors.Is compares against. Timeout() bool Whether a deadline expired. Temporary() bool Deprecated. Do not branch on it. Satisfies error, net.Error Almost every error from this package is one of these. | |
type DNSError struct4 methods | A failed lookup. Err, Name, IsTimeout, IsNotFound. |
Error() string The message, naming the host that failed. Unwrap() error The underlying error. Timeout() bool Whether the lookup timed out. Temporary() bool Deprecated. Satisfies error, net.Error
| |
type AddrError struct3 methods | A malformed address. Err and Addr. |
Error() string The message and the address, "missing port in address". Timeout() bool Always false. A bad address is not a timeout. Temporary() bool Deprecated. Satisfies error, net.Error What you get for malformed input rather than a failed connection. The message names the problem and the string that caused it. | |
Values
| Value | Description |
|---|---|
ErrClosed error errors.Is(err, net.ErrClosed) | The socket was already closed. |
| |
DefaultResolver *Resolver net.DefaultResolver.LookupHost(ctx, h) | The resolver every package-level Lookup uses. |
| |
IPv4len, IPv6len int net.CIDRMask(24, 8*net.IPv4len) | 4 and 16, the two address widths. |
IPv4zero, IPv6zero IP ip.Equal(net.IPv4zero) | The unspecified addresses, 0.0.0.0 and ::. |
IPv4bcast IP net.IPv4bcast | The limited broadcast address, 255.255.255.255. |
IPv6loopback IP ip.Equal(net.IPv6loopback) | ::1. |
FlagUp, FlagLoopback, FlagBroadcast, FlagMulticast Flags ifi.Flags&net.FlagUp != 0 | The interface status bits. |
| |
ErrWriteToConnected error errors.Is(err, net.ErrWriteToConnected) | WriteTo was called on a connected socket. |
| |
net/http is built entirely on top of this package: http.ListenAndServe calls net.Listen internally and hands each accepted connection to its own request-handling goroutine.