Overview
Package net provides a portable interface for network I/O, including TCP/IP, UDP, domain name resolution, and Unix domain sockets. Although the package provides access to low-level networking primitives, most clients will need only the basic interface provided by the Dial, Listen, and Accept functions and the associated Conn and Listener interfaces.Key Functions
Dial
Connects to a server.The network type: “tcp”, “tcp4” (IPv4-only), “tcp6” (IPv6-only), “udp”, “udp4”, “udp6”, “ip”, “ip4”, “ip6”, “unix”, “unixgram”, “unixpacket”
The address to dial. For TCP and UDP networks, addresses have the form
host:port. If host is a literal IPv6 address it must be enclosed in square brackets as in [::1]:80A generic stream-oriented network connection
Error if connection failed
Listen
Creates a network listener on a local network address.The network type: “tcp”, “tcp4”, “tcp6”, “unix”, “unixpacket”
The local address to listen on. For TCP networks, if the host is empty or a literal unspecified IP address, Listen listens on all available unicast and anycast IP addresses
A network listener for stream-oriented protocols
Error if listener creation failed
Core Interfaces
Conn
Conn is a generic stream-oriented network connection. Multiple goroutines may invoke methods on a Conn simultaneously.Methods
Reads data from the connection. Can be made to time out and return an error after a fixed time limit
Writes data to the connection. Can be made to time out and return an error after a fixed time limit
Closes the connection. Any blocked Read or Write operations will be unblocked and return errors
Sets the read and write deadlines. A zero value for t means I/O operations will not time out
Addr
Addr represents a network end point address.Listener
Listener is a generic network listener for stream-oriented protocols.Waits for and returns the next connection to the listener
Types
TCPAddr
TCPAddr represents the address of a TCP end point.The IP address
The port number
IPv6 scoped addressing zone
TCPConn
TCPConn is an implementation of the Conn interface for TCP network connections.Name Resolution
LookupHost
Looks up the given host using the local resolver.The hostname to look up
A slice of the host’s addresses
Error if lookup failed