# package net
`import "net"`
net包提供了可移植的網絡I/O接口,包括TCP/IP、UDP、域名解析和Unix域socket。
雖然本包提供了對網絡原語的訪問,大部分使用者只需要Dial、Listen和Accept函數提供的基本接口;以及相關的Conn和Listener接口。crypto/tls包提供了相同的接口和類似的Dial和Listen函數。
Dial函數和服務端建立連接:
```
conn, err := net.Dial("tcp", "google.com:80")
if err != nil {
// handle error
}
fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
status, err := bufio.NewReader(conn).ReadString('\n')
// ...
```
Listen函數創建的服務端:
```
ln, err := net.Listen("tcp", ":8080")
if err != nil {
// handle error
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
continue
}
go handleConnection(conn)
}
```
## Index
* [Constants](#pkg-constants)
* [Variables](#pkg-variables)
* [type ParseError](#ParseError)
* [func (e \*ParseError) Error() string](#ParseError.Error)
* [type Error](#Error)
* [type InvalidAddrError](#InvalidAddrError)
* [func (e InvalidAddrError) Error() string](#InvalidAddrError.Error)
* [func (e InvalidAddrError) Temporary() bool](#InvalidAddrError.Temporary)
* [func (e InvalidAddrError) Timeout() bool](#InvalidAddrError.Timeout)
* [type UnknownNetworkError](#UnknownNetworkError)
* [func (e UnknownNetworkError) Error() string](#UnknownNetworkError.Error)
* [func (e UnknownNetworkError) Temporary() bool](#UnknownNetworkError.Temporary)
* [func (e UnknownNetworkError) Timeout() bool](#UnknownNetworkError.Timeout)
* [type DNSConfigError](#DNSConfigError)
* [func (e \*DNSConfigError) Error() string](#DNSConfigError.Error)
* [func (e \*DNSConfigError) Temporary() bool](#DNSConfigError.Temporary)
* [func (e \*DNSConfigError) Timeout() bool](#DNSConfigError.Timeout)
* [type DNSError](#DNSError)
* [func (e \*DNSError) Error() string](#DNSError.Error)
* [func (e \*DNSError) Temporary() bool](#DNSError.Temporary)
* [func (e \*DNSError) Timeout() bool](#DNSError.Timeout)
* [type AddrError](#AddrError)
* [func (e \*AddrError) Error() string](#AddrError.Error)
* [func (e \*AddrError) Temporary() bool](#AddrError.Temporary)
* [func (e \*AddrError) Timeout() bool](#AddrError.Timeout)
* [type OpError](#OpError)
* [func (e \*OpError) Error() string](#OpError.Error)
* [func (e \*OpError) Temporary() bool](#OpError.Temporary)
* [func (e \*OpError) Timeout() bool](#OpError.Timeout)
* [func SplitHostPort(hostport string) (host, port string, err error)](#SplitHostPort)
* [func JoinHostPort(host, port string) string](#JoinHostPort)
* [type HardwareAddr](#HardwareAddr)
* [func ParseMAC(s string) (hw HardwareAddr, err error)](#ParseMAC)
* [func (a HardwareAddr) String() string](#HardwareAddr.String)
* [type Flags](#Flags)
* [func (f Flags) String() string](#Flags.String)
* [type Interface](#Interface)
* [func InterfaceByIndex(index int) (\*Interface, error)](#InterfaceByIndex)
* [func InterfaceByName(name string) (\*Interface, error)](#InterfaceByName)
* [func (ifi \*Interface) Addrs() ([]Addr, error)](#Interface.Addrs)
* [func (ifi \*Interface) MulticastAddrs() ([]Addr, error)](#Interface.MulticastAddrs)
* [func Interfaces() ([]Interface, error)](#Interfaces)
* [func InterfaceAddrs() ([]Addr, error)](#InterfaceAddrs)
* [type IP](#IP)
* [func IPv4(a, b, c, d byte) IP](#IPv4)
* [func ParseIP(s string) IP](#ParseIP)
* [func (ip IP) IsGlobalUnicast() bool](#IP.IsGlobalUnicast)
* [func (ip IP) IsLinkLocalUnicast() bool](#IP.IsLinkLocalUnicast)
* [func (ip IP) IsInterfaceLocalMulticast() bool](#IP.IsInterfaceLocalMulticast)
* [func (ip IP) IsLinkLocalMulticast() bool](#IP.IsLinkLocalMulticast)
* [func (ip IP) IsMulticast() bool](#IP.IsMulticast)
* [func (ip IP) IsLoopback() bool](#IP.IsLoopback)
* [func (ip IP) IsUnspecified() bool](#IP.IsUnspecified)
* [func (ip IP) DefaultMask() IPMask](#IP.DefaultMask)
* [func (ip IP) Equal(x IP) bool](#IP.Equal)
* [func (ip IP) To16() IP](#IP.To16)
* [func (ip IP) To4() IP](#IP.To4)
* [func (ip IP) Mask(mask IPMask) IP](#IP.Mask)
* [func (ip IP) String() string](#IP.String)
* [func (ip IP) MarshalText() ([]byte, error)](#IP.MarshalText)
* [func (ip \*IP) UnmarshalText(text []byte) error](#IP.UnmarshalText)
* [type IPMask](#IPMask)
* [func IPv4Mask(a, b, c, d byte) IPMask](#IPv4Mask)
* [func CIDRMask(ones, bits int) IPMask](#CIDRMask)
* [func (m IPMask) Size() (ones, bits int)](#IPMask.Size)
* [func (m IPMask) String() string](#IPMask.String)
* [type IPNet](#IPNet)
* [func ParseCIDR(s string) (IP, \*IPNet, error)](#ParseCIDR)
* [func (n \*IPNet) Contains(ip IP) bool](#IPNet.Contains)
* [func (n \*IPNet) Network() string](#IPNet.Network)
* [func (n \*IPNet) String() string](#IPNet.String)
* [type Addr](#Addr)
* [type Conn](#Conn)
* [func Dial(network, address string) (Conn, error)](#Dial)
* [func DialTimeout(network, address string, timeout time.Duration) (Conn, error)](#DialTimeout)
* [func Pipe() (Conn, Conn)](#Pipe)
* [type PacketConn](#PacketConn)
* [func ListenPacket(net, laddr string) (PacketConn, error)](#ListenPacket)
* [type Dialer](#Dialer)
* [func (d \*Dialer) Dial(network, address string) (Conn, error)](#Dialer.Dial)
* [type Listener](#Listener)
* [func Listen(net, laddr string) (Listener, error)](#Listen)
* [type IPAddr](#IPAddr)
* [func ResolveIPAddr(net, addr string) (\*IPAddr, error)](#ResolveIPAddr)
* [func (a \*IPAddr) Network() string](#IPAddr.Network)
* [func (a \*IPAddr) String() string](#IPAddr.String)
* [type TCPAddr](#TCPAddr)
* [func ResolveTCPAddr(net, addr string) (\*TCPAddr, error)](#ResolveTCPAddr)
* [func (a \*TCPAddr) Network() string](#TCPAddr.Network)
* [func (a \*TCPAddr) String() string](#TCPAddr.String)
* [type UDPAddr](#UDPAddr)
* [func ResolveUDPAddr(net, addr string) (\*UDPAddr, error)](#ResolveUDPAddr)
* [func (a \*UDPAddr) Network() string](#UDPAddr.Network)
* [func (a \*UDPAddr) String() string](#UDPAddr.String)
* [type UnixAddr](#UnixAddr)
* [func ResolveUnixAddr(net, addr string) (\*UnixAddr, error)](#ResolveUnixAddr)
* [func (a \*UnixAddr) Network() string](#UnixAddr.Network)
* [func (a \*UnixAddr) String() string](#UnixAddr.String)
* [type IPConn](#IPConn)
* [func DialIP(netProto string, laddr, raddr \*IPAddr) (\*IPConn, error)](#DialIP)
* [func ListenIP(netProto string, laddr \*IPAddr) (\*IPConn, error)](#ListenIP)
* [func (c \*IPConn) LocalAddr() Addr](#IPConn.LocalAddr)
* [func (c \*IPConn) RemoteAddr() Addr](#IPConn.RemoteAddr)
* [func (c \*IPConn) SetReadBuffer(bytes int) error](#IPConn.SetReadBuffer)
* [func (c \*IPConn) SetWriteBuffer(bytes int) error](#IPConn.SetWriteBuffer)
* [func (c \*IPConn) SetDeadline(t time.Time) error](#IPConn.SetDeadline)
* [func (c \*IPConn) SetReadDeadline(t time.Time) error](#IPConn.SetReadDeadline)
* [func (c \*IPConn) SetWriteDeadline(t time.Time) error](#IPConn.SetWriteDeadline)
* [func (c \*IPConn) Read(b []byte) (int, error)](#IPConn.Read)
* [func (c \*IPConn) ReadFrom(b []byte) (int, Addr, error)](#IPConn.ReadFrom)
* [func (c \*IPConn) ReadFromIP(b []byte) (int, \*IPAddr, error)](#IPConn.ReadFromIP)
* [func (c \*IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr \*IPAddr, err error)](#IPConn.ReadMsgIP)
* [func (c \*IPConn) Write(b []byte) (int, error)](#IPConn.Write)
* [func (c \*IPConn) WriteTo(b []byte, addr Addr) (int, error)](#IPConn.WriteTo)
* [func (c \*IPConn) WriteToIP(b []byte, addr \*IPAddr) (int, error)](#IPConn.WriteToIP)
* [func (c \*IPConn) WriteMsgIP(b, oob []byte, addr \*IPAddr) (n, oobn int, err error)](#IPConn.WriteMsgIP)
* [func (c \*IPConn) Close() error](#IPConn.Close)
* [func (c \*IPConn) File() (f \*os.File, err error)](#IPConn.File)
* [type TCPConn](#TCPConn)
* [func DialTCP(net string, laddr, raddr \*TCPAddr) (\*TCPConn, error)](#DialTCP)
* [func (c \*TCPConn) LocalAddr() Addr](#TCPConn.LocalAddr)
* [func (c \*TCPConn) RemoteAddr() Addr](#TCPConn.RemoteAddr)
* [func (c \*TCPConn) SetReadBuffer(bytes int) error](#TCPConn.SetReadBuffer)
* [func (c \*TCPConn) SetWriteBuffer(bytes int) error](#TCPConn.SetWriteBuffer)
* [func (c \*TCPConn) SetDeadline(t time.Time) error](#TCPConn.SetDeadline)
* [func (c \*TCPConn) SetReadDeadline(t time.Time) error](#TCPConn.SetReadDeadline)
* [func (c \*TCPConn) SetWriteDeadline(t time.Time) error](#TCPConn.SetWriteDeadline)
* [func (c \*TCPConn) SetKeepAlive(keepalive bool) error](#TCPConn.SetKeepAlive)
* [func (c \*TCPConn) SetKeepAlivePeriod(d time.Duration) error](#TCPConn.SetKeepAlivePeriod)
* [func (c \*TCPConn) SetLinger(sec int) error](#TCPConn.SetLinger)
* [func (c \*TCPConn) SetNoDelay(noDelay bool) error](#TCPConn.SetNoDelay)
* [func (c \*TCPConn) Read(b []byte) (int, error)](#TCPConn.Read)
* [func (c \*TCPConn) ReadFrom(r io.Reader) (int64, error)](#TCPConn.ReadFrom)
* [func (c \*TCPConn) Write(b []byte) (int, error)](#TCPConn.Write)
* [func (c \*TCPConn) Close() error](#TCPConn.Close)
* [func (c \*TCPConn) CloseRead() error](#TCPConn.CloseRead)
* [func (c \*TCPConn) CloseWrite() error](#TCPConn.CloseWrite)
* [func (c \*TCPConn) File() (f \*os.File, err error)](#TCPConn.File)
* [type UDPConn](#UDPConn)
* [func DialUDP(net string, laddr, raddr \*UDPAddr) (\*UDPConn, error)](#DialUDP)
* [func ListenMulticastUDP(net string, ifi \*Interface, gaddr \*UDPAddr) (\*UDPConn, error)](#ListenMulticastUDP)
* [func ListenUDP(net string, laddr \*UDPAddr) (\*UDPConn, error)](#ListenUDP)
* [func (c \*UDPConn) LocalAddr() Addr](#UDPConn.LocalAddr)
* [func (c \*UDPConn) RemoteAddr() Addr](#UDPConn.RemoteAddr)
* [func (c \*UDPConn) SetReadBuffer(bytes int) error](#UDPConn.SetReadBuffer)
* [func (c \*UDPConn) SetWriteBuffer(bytes int) error](#UDPConn.SetWriteBuffer)
* [func (c \*UDPConn) SetDeadline(t time.Time) error](#UDPConn.SetDeadline)
* [func (c \*UDPConn) SetReadDeadline(t time.Time) error](#UDPConn.SetReadDeadline)
* [func (c \*UDPConn) SetWriteDeadline(t time.Time) error](#UDPConn.SetWriteDeadline)
* [func (c \*UDPConn) Read(b []byte) (int, error)](#UDPConn.Read)
* [func (c \*UDPConn) ReadFrom(b []byte) (int, Addr, error)](#UDPConn.ReadFrom)
* [func (c \*UDPConn) ReadFromUDP(b []byte) (n int, addr \*UDPAddr, err error)](#UDPConn.ReadFromUDP)
* [func (c \*UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr \*UDPAddr, err error)](#UDPConn.ReadMsgUDP)
* [func (c \*UDPConn) Write(b []byte) (int, error)](#UDPConn.Write)
* [func (c \*UDPConn) WriteTo(b []byte, addr Addr) (int, error)](#UDPConn.WriteTo)
* [func (c \*UDPConn) WriteToUDP(b []byte, addr \*UDPAddr) (int, error)](#UDPConn.WriteToUDP)
* [func (c \*UDPConn) WriteMsgUDP(b, oob []byte, addr \*UDPAddr) (n, oobn int, err error)](#UDPConn.WriteMsgUDP)
* [func (c \*UDPConn) Close() error](#UDPConn.Close)
* [func (c \*UDPConn) File() (f \*os.File, err error)](#UDPConn.File)
* [type UnixConn](#UnixConn)
* [func DialUnix(net string, laddr, raddr \*UnixAddr) (\*UnixConn, error)](#DialUnix)
* [func ListenUnixgram(net string, laddr \*UnixAddr) (\*UnixConn, error)](#ListenUnixgram)
* [func (c \*UnixConn) LocalAddr() Addr](#UnixConn.LocalAddr)
* [func (c \*UnixConn) RemoteAddr() Addr](#UnixConn.RemoteAddr)
* [func (c \*UnixConn) SetReadBuffer(bytes int) error](#UnixConn.SetReadBuffer)
* [func (c \*UnixConn) SetWriteBuffer(bytes int) error](#UnixConn.SetWriteBuffer)
* [func (c \*UnixConn) SetDeadline(t time.Time) error](#UnixConn.SetDeadline)
* [func (c \*UnixConn) SetReadDeadline(t time.Time) error](#UnixConn.SetReadDeadline)
* [func (c \*UnixConn) SetWriteDeadline(t time.Time) error](#UnixConn.SetWriteDeadline)
* [func (c \*UnixConn) Read(b []byte) (int, error)](#UnixConn.Read)
* [func (c \*UnixConn) ReadFrom(b []byte) (int, Addr, error)](#UnixConn.ReadFrom)
* [func (c \*UnixConn) ReadFromUnix(b []byte) (n int, addr \*UnixAddr, err error)](#UnixConn.ReadFromUnix)
* [func (c \*UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr \*UnixAddr, err error)](#UnixConn.ReadMsgUnix)
* [func (c \*UnixConn) Write(b []byte) (int, error)](#UnixConn.Write)
* [func (c \*UnixConn) WriteTo(b []byte, addr Addr) (n int, err error)](#UnixConn.WriteTo)
* [func (c \*UnixConn) WriteToUnix(b []byte, addr \*UnixAddr) (n int, err error)](#UnixConn.WriteToUnix)
* [func (c \*UnixConn) WriteMsgUnix(b, oob []byte, addr \*UnixAddr) (n, oobn int, err error)](#UnixConn.WriteMsgUnix)
* [func (c \*UnixConn) Close() error](#UnixConn.Close)
* [func (c \*UnixConn) CloseRead() error](#UnixConn.CloseRead)
* [func (c \*UnixConn) CloseWrite() error](#UnixConn.CloseWrite)
* [func (c \*UnixConn) File() (f \*os.File, err error)](#UnixConn.File)
* [type TCPListener](#TCPListener)
* [func ListenTCP(net string, laddr \*TCPAddr) (\*TCPListener, error)](#ListenTCP)
* [func (l \*TCPListener) Addr() Addr](#TCPListener.Addr)
* [func (l \*TCPListener) SetDeadline(t time.Time) error](#TCPListener.SetDeadline)
* [func (l \*TCPListener) Accept() (Conn, error)](#TCPListener.Accept)
* [func (l \*TCPListener) AcceptTCP() (\*TCPConn, error)](#TCPListener.AcceptTCP)
* [func (l \*TCPListener) Close() error](#TCPListener.Close)
* [func (l \*TCPListener) File() (f \*os.File, err error)](#TCPListener.File)
* [type UnixListener](#UnixListener)
* [func ListenUnix(net string, laddr \*UnixAddr) (\*UnixListener, error)](#ListenUnix)
* [func (l \*UnixListener) Addr() Addr](#UnixListener.Addr)
* [func (l \*UnixListener) SetDeadline(t time.Time) (err error)](#UnixListener.SetDeadline)
* [func (l \*UnixListener) Accept() (c Conn, err error)](#UnixListener.Accept)
* [func (l \*UnixListener) AcceptUnix() (\*UnixConn, error)](#UnixListener.AcceptUnix)
* [func (l \*UnixListener) Close() error](#UnixListener.Close)
* [func (l \*UnixListener) File() (f \*os.File, err error)](#UnixListener.File)
* [func FileConn(f \*os.File) (c Conn, err error)](#FileConn)
* [func FilePacketConn(f \*os.File) (c PacketConn, err error)](#FilePacketConn)
* [func FileListener(f \*os.File) (l Listener, err error)](#FileListener)
* [type MX](#MX)
* [type NS](#NS)
* [type SRV](#SRV)
* [func LookupPort(network, service string) (port int, err error)](#LookupPort)
* [func LookupCNAME(name string) (cname string, err error)](#LookupCNAME)
* [func LookupHost(host string) (addrs []string, err error)](#LookupHost)
* [func LookupIP(host string) (addrs []IP, err error)](#LookupIP)
* [func LookupAddr(addr string) (name []string, err error)](#LookupAddr)
* [func LookupMX(name string) (mx []\*MX, err error)](#LookupMX)
* [func LookupNS(name string) (ns []\*NS, err error)](#LookupNS)
* [func LookupSRV(service, proto, name string) (cname string, addrs []\*SRV, err error)](#LookupSRV)
* [func LookupTXT(name string) (txt []string, err error)](#LookupTXT)
### Examples
* [Listener](#example-Listener)
```
const (
IPv4len = 4
IPv6len = 16
)
```
IP address lengths (bytes).
## Variables
```
var (
IPv4bcast = IPv4(255, 255, 255, 255) // 廣播地址
IPv4allsys = IPv4(224, 0, 0, 1) // 所有主機和路由器
IPv4allrouter = IPv4(224, 0, 0, 2) // 所有路由器
IPv4zero = IPv4(0, 0, 0, 0) // 本地地址,只能作為源地址(曾用作廣播地址)
)
```
常用的IPv4地址。
```
var (
IPv6zero = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
IPv6unspecified = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
IPv6loopback = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
IPv6interfacelocalallnodes = IP{0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
IPv6linklocalallnodes = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
IPv6linklocalallrouters = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02}
)
```
常用的IPv6地址。
```
var (
ErrWriteToConnected = errors.New("use of WriteTo with pre-connected connection")
)
```
很多OpError類型的錯誤會包含本錯誤。
## type [ParseError](https://github.com/golang/go/blob/master/src/net/ip.go#L634 "View Source")
```
type ParseError struct {
Type string
Text string
}
```
ParseError代表一個格式錯誤的字符串,Type為期望的格式。
### func (\*ParseError) [Error](https://github.com/golang/go/blob/master/src/net/ip.go#L639 "View Source")
```
func (e *ParseError) Error() string
```
## type [Error](https://github.com/golang/go/blob/master/src/net/net.go#L209 "View Source")
```
type Error interface {
error
Timeout() bool // 錯誤是否為超時?
Temporary() bool // 錯誤是否是臨時的?
}
```
Error代表一個網絡錯誤。
## type [UnknownNetworkError](https://github.com/golang/go/blob/master/src/net/net.go#L373 "View Source")
```
type UnknownNetworkError string
```
### func (UnknownNetworkError) [Error](https://github.com/golang/go/blob/master/src/net/net.go#L375 "View Source")
```
func (e UnknownNetworkError) Error() string
```
### func (UnknownNetworkError) [Temporary](https://github.com/golang/go/blob/master/src/net/net.go#L376 "View Source")
```
func (e UnknownNetworkError) Temporary() bool
```
### func (UnknownNetworkError) [Timeout](https://github.com/golang/go/blob/master/src/net/net.go#L377 "View Source")
```
func (e UnknownNetworkError) Timeout() bool
```
## type [InvalidAddrError](https://github.com/golang/go/blob/master/src/net/net.go#L379 "View Source")
```
type InvalidAddrError string
```
### func (InvalidAddrError) [Error](https://github.com/golang/go/blob/master/src/net/net.go#L381 "View Source")
```
func (e InvalidAddrError) Error() string
```
### func (InvalidAddrError) [Temporary](https://github.com/golang/go/blob/master/src/net/net.go#L383 "View Source")
```
func (e InvalidAddrError) Temporary() bool
```
### func (InvalidAddrError) [Timeout](https://github.com/golang/go/blob/master/src/net/net.go#L382 "View Source")
```
func (e InvalidAddrError) Timeout() bool
```
## type [DNSConfigError](https://github.com/golang/go/blob/master/src/net/net.go#L386 "View Source")
```
type DNSConfigError struct {
Err error
}
```
DNSConfigError代表讀取主機DNS配置時出現的錯誤。
### func (\*DNSConfigError) [Error](https://github.com/golang/go/blob/master/src/net/net.go#L390 "View Source")
```
func (e *DNSConfigError) Error() string
```
### func (\*DNSConfigError) [Temporary](https://github.com/golang/go/blob/master/src/net/net.go#L395 "View Source")
```
func (e *DNSConfigError) Temporary() bool
```
### func (\*DNSConfigError) [Timeout](https://github.com/golang/go/blob/master/src/net/net.go#L394 "View Source")
```
func (e *DNSConfigError) Timeout() bool
```
## type [DNSError](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L13 "View Source")
```
type DNSError struct {
Err string // 錯誤的描述
Name string // 查詢的名稱
Server string // 使用的服務器
IsTimeout bool
}
```
DNSError代表DNS查詢的錯誤。
### func (\*DNSError) [Error](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L20 "View Source")
```
func (e *DNSError) Error() string
```
### func (\*DNSError) [Temporary](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L33 "View Source")
```
func (e *DNSError) Temporary() bool
```
### func (\*DNSError) [Timeout](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L32 "View Source")
```
func (e *DNSError) Timeout() bool
```
## type [AddrError](https://github.com/golang/go/blob/master/src/net/net.go#L349 "View Source")
```
type AddrError struct {
Err string
Addr string
}
```
### func (\*AddrError) [Error](https://github.com/golang/go/blob/master/src/net/net.go#L354 "View Source")
```
func (e *AddrError) Error() string
```
### func (\*AddrError) [Temporary](https://github.com/golang/go/blob/master/src/net/net.go#L365 "View Source")
```
func (e *AddrError) Temporary() bool
```
### func (\*AddrError) [Timeout](https://github.com/golang/go/blob/master/src/net/net.go#L369 "View Source")
```
func (e *AddrError) Timeout() bool
```
## type [OpError](https://github.com/golang/go/blob/master/src/net/net.go#L292 "View Source")
```
type OpError struct {
// Op是出現錯誤的操作,如"read"或"write"
Op string
// Net是錯誤所在的網絡類型,如"tcp"或"udp6"
Net string
// Addr是出現錯誤的網絡地址
Addr Addr
// Err是操作中出現的錯誤
Err error
}
```
OpError是經常被net包的函數返回的錯誤類型。它描述了該錯誤的操作、網絡類型和網絡地址。
### func (\*OpError) [Error](https://github.com/golang/go/blob/master/src/net/net.go#L308 "View Source")
```
func (e *OpError) Error() string
```
### func (\*OpError) [Temporary](https://github.com/golang/go/blob/master/src/net/net.go#L327 "View Source")
```
func (e *OpError) Temporary() bool
```
### func (\*OpError) [Timeout](https://github.com/golang/go/blob/master/src/net/net.go#L338 "View Source")
```
func (e *OpError) Timeout() bool
```
## func [SplitHostPort](https://github.com/golang/go/blob/master/src/net/ipsock.go#L143 "View Source")
```
func SplitHostPort(hostport string) (host, port string, err error)
```
函數將格式為"host:port"、"[host]:port"或"[ipv6-host%zone]:port"的網絡地址分割為host或ipv6-host%zone和port兩個部分。Ipv6的文字地址或者主機名必須用方括號括起來,如"[::1]:80"、"[ipv6-host]:http"、"[ipv6-host%zone]:80"。
## func [JoinHostPort](https://github.com/golang/go/blob/master/src/net/ipsock.go#L223 "View Source")
```
func JoinHostPort(host, port string) string
```
函數將host和port合并為一個網絡地址。一般格式為"host:port";如果host含有冒號或百分號,格式為"[host]:port"。
## type [HardwareAddr](https://github.com/golang/go/blob/master/src/net/mac.go#L14 "View Source")
```
type HardwareAddr []byte
```
HardwareAddr類型代表一個硬件地址(MAC地址)。
### func [ParseMAC](https://github.com/golang/go/blob/master/src/net/mac.go#L39 "View Source")
```
func ParseMAC(s string) (hw HardwareAddr, err error)
```
ParseMAC函數使用如下格式解析一個IEEE 802 MAC-48、EUI-48或EUI-64硬件地址:
```
01:23:45:67:89:ab
01:23:45:67:89:ab:cd:ef
01-23-45-67-89-ab
01-23-45-67-89-ab-cd-ef
0123.4567.89ab
0123.4567.89ab.cdef
```
### func (HardwareAddr) [String](https://github.com/golang/go/blob/master/src/net/mac.go#L16 "View Source")
```
func (a HardwareAddr) String() string
```
## type [Flags](https://github.com/golang/go/blob/master/src/net/interface.go#L28 "View Source")
```
type Flags uint
```
```
const (
FlagUp Flags = 1 << iota // 接口在活動狀態
FlagBroadcast // 接口支持廣播
FlagLoopback // 接口是環回的
FlagPointToPoint // 接口是點對點的
FlagMulticast // 接口支持組播
)
```
### func (Flags) [String](https://github.com/golang/go/blob/master/src/net/interface.go#L46 "View Source")
```
func (f Flags) String() string
```
## type [Interface](https://github.com/golang/go/blob/master/src/net/interface.go#L20 "View Source")
```
type Interface struct {
Index int // 索引,>=1的整數
MTU int // 最大傳輸單元
Name string // 接口名,例如"en0"、"lo0"、"eth0.100"
HardwareAddr HardwareAddr // 硬件地址,IEEE MAC-48、EUI-48或EUI-64格式
Flags Flags // 接口的屬性,例如FlagUp、FlagLoopback、FlagMulticast
}
```
Interface類型代表一個網絡接口(系統與網絡的一個接點)。包含接口索引到名字的映射,也包含接口的設備信息。
### func [InterfaceByIndex](https://github.com/golang/go/blob/master/src/net/interface.go#L91 "View Source")
```
func InterfaceByIndex(index int) (*Interface, error)
```
InterfaceByIndex返回指定索引的網絡接口。
### func [InterfaceByName](https://github.com/golang/go/blob/master/src/net/interface.go#L112 "View Source")
```
func InterfaceByName(name string) (*Interface, error)
```
InterfaceByName返回指定名字的網絡接口。
### func (\*Interface) [Addrs](https://github.com/golang/go/blob/master/src/net/interface.go#L63 "View Source")
```
func (ifi *Interface) Addrs() ([]Addr, error)
```
Addrs方法返回網絡接口ifi的一或多個接口地址。
### func (\*Interface) [MulticastAddrs](https://github.com/golang/go/blob/master/src/net/interface.go#L72 "View Source")
```
func (ifi *Interface) MulticastAddrs() ([]Addr, error)
```
MulticastAddrs返回網絡接口ifi加入的多播組地址。
## func [Interfaces](https://github.com/golang/go/blob/master/src/net/interface.go#L80 "View Source")
```
func Interfaces() ([]Interface, error)
```
Interfaces返回該系統的網絡接口列表。
## func [InterfaceAddrs](https://github.com/golang/go/blob/master/src/net/interface.go#L86 "View Source")
```
func InterfaceAddrs() ([]Addr, error)
```
InterfaceAddrs返回該系統的網絡接口的地址列表。
## type [IP](https://github.com/golang/go/blob/master/src/net/ip.go#L32 "View Source")
```
type IP []byte
```
IP類型是代表單個IP地址的[]byte切片。本包的函數都可以接受4字節(IPv4)和16字節(IPv6)的切片作為輸入。
注意,IP地址是IPv4地址還是IPv6地址是語義上的屬性,而不取決于切片的長度:16字節的切片也可以是IPv4地址。
### func [IPv4](https://github.com/golang/go/blob/master/src/net/ip.go#L45 "View Source")
```
func IPv4(a, b, c, d byte) IP
```
IPv4返回包含一個IPv4地址a.b.c.d的IP地址(16字節格式)。
### func [ParseIP](https://github.com/golang/go/blob/master/src/net/ip.go#L648 "View Source")
```
func ParseIP(s string) IP
```
ParseIP將s解析為IP地址,并返回該地址。如果s不是合法的IP地址文本表示,ParseIP會返回nil。
字符串可以是小數點分隔的IPv4格式(如"74.125.19.99")或IPv6格式(如"2001:4860:0:2001::68")格式。
### func (IP) [IsGlobalUnicast](https://github.com/golang/go/blob/master/src/net/ip.go#L161 "View Source")
```
func (ip IP) IsGlobalUnicast() bool
```
如果ip是全局單播地址,則返回真。
### func (IP) [IsLinkLocalUnicast](https://github.com/golang/go/blob/master/src/net/ip.go#L152 "View Source")
```
func (ip IP) IsLinkLocalUnicast() bool
```
如果ip是鏈路本地單播地址,則返回真。
### func (IP) [IsInterfaceLocalMulticast](https://github.com/golang/go/blob/master/src/net/ip.go#L137 "View Source")
```
func (ip IP) IsInterfaceLocalMulticast() bool
```
如果ip是接口本地組播地址,則返回真。
### func (IP) [IsLinkLocalMulticast](https://github.com/golang/go/blob/master/src/net/ip.go#L143 "View Source")
```
func (ip IP) IsLinkLocalMulticast() bool
```
如果ip是鏈路本地組播地址,則返回真。
### func (IP) [IsMulticast](https://github.com/golang/go/blob/master/src/net/ip.go#L128 "View Source")
```
func (ip IP) IsMulticast() bool
```
如果ip是組播地址,則返回真。
### func (IP) [IsLoopback](https://github.com/golang/go/blob/master/src/net/ip.go#L120 "View Source")
```
func (ip IP) IsLoopback() bool
```
如果ip是環回地址,則返回真。
### func (IP) [IsUnspecified](https://github.com/golang/go/blob/master/src/net/ip.go#L112 "View Source")
```
func (ip IP) IsUnspecified() bool
```
如果ip是未指定地址,則返回真。
### func (IP) [DefaultMask](https://github.com/golang/go/blob/master/src/net/ip.go#L215 "View Source")
```
func (ip IP) DefaultMask() IPMask
```
函數返回IP地址ip的默認子網掩碼。只有IPv4有默認子網掩碼;如果ip不是合法的IPv4地址,會返回nil。
### func (IP) [Equal](https://github.com/golang/go/blob/master/src/net/ip.go#L355 "View Source")
```
func (ip IP) Equal(x IP) bool
```
如果ip和x代表同一個IP地址,Equal會返回真。代表同一地址的IPv4地址和IPv6地址也被認為是相等的。
### func (IP) [To16](https://github.com/golang/go/blob/master/src/net/ip.go#L195 "View Source")
```
func (ip IP) To16() IP
```
To16將一個IP地址轉換為16字節表示。如果ip不是一個IP地址(長度錯誤),To16會返回nil。
### func (IP) [To4](https://github.com/golang/go/blob/master/src/net/ip.go#L180 "View Source")
```
func (ip IP) To4() IP
```
To4將一個IPv4地址轉換為4字節表示。如果ip不是IPv4地址,To4會返回nil。
### func (IP) [Mask](https://github.com/golang/go/blob/master/src/net/ip.go#L239 "View Source")
```
func (ip IP) Mask(mask IPMask) IP
```
Mask方法認為mask為ip的子網掩碼,返回ip的網絡地址部分的ip。(主機地址部分都置0)
### func (IP) [String](https://github.com/golang/go/blob/master/src/net/ip.go#L261 "View Source")
```
func (ip IP) String() string
```
String返回IP地址ip的字符串表示。如果ip是IPv4地址,返回值的格式為點分隔的,如"74.125.19.99";否則表示為IPv6格式,如"2001:4860:0:2001::68"。
### func (IP) [MarshalText](https://github.com/golang/go/blob/master/src/net/ip.go#L326 "View Source")
```
func (ip IP) MarshalText() ([]byte, error)
```
MarshalText實現了encoding.TextMarshaler接口,返回值和String方法一樣。
### func (\*IP) [UnmarshalText](https://github.com/golang/go/blob/master/src/net/ip.go#L338 "View Source")
```
func (ip *IP) UnmarshalText(text []byte) error
```
UnmarshalText實現了encoding.TextUnmarshaler接口。IP地址字符串應該是ParseIP函數可以接受的格式。
## type [IPMask](https://github.com/golang/go/blob/master/src/net/ip.go#L35 "View Source")
```
type IPMask []byte
```
IPMask代表一個IP地址的掩碼。
### func [IPv4Mask](https://github.com/golang/go/blob/master/src/net/ip.go#L59 "View Source")
```
func IPv4Mask(a, b, c, d byte) IPMask
```
IPv4Mask返回一個4字節格式的IPv4掩碼a.b.c.d。
### func [CIDRMask](https://github.com/golang/go/blob/master/src/net/ip.go#L71 "View Source")
```
func CIDRMask(ones, bits int) IPMask
```
CIDRMask返回一個IPMask類型值,該返回值總共有bits個字位,其中前ones個字位都是1,其余字位都是0。
### func (IPMask) [Size](https://github.com/golang/go/blob/master/src/net/ip.go#L412 "View Source")
```
func (m IPMask) Size() (ones, bits int)
```
Size返回m的前導的1字位數和總字位數。如果m不是規范的子網掩碼(字位:/^1+0+$/),將返會(0, 0)。
### func (IPMask) [String](https://github.com/golang/go/blob/master/src/net/ip.go#L421 "View Source")
```
func (m IPMask) String() string
```
String返回m的十六進制格式,沒有標點。
## type [IPNet](https://github.com/golang/go/blob/master/src/net/ip.go#L38 "View Source")
```
type IPNet struct {
IP IP // 網絡地址
Mask IPMask // 子網掩碼
}
```
IPNet表示一個IP網絡。
### func [ParseCIDR](https://github.com/golang/go/blob/master/src/net/ip.go#L663 "View Source")
```
func ParseCIDR(s string) (IP, *IPNet, error)
```
ParseCIDR將s作為一個CIDR(無類型域間路由)的IP地址和掩碼字符串,如"192.168.100.1/24"或"2001:DB8::/48",解析并返回IP地址和IP網絡,參見[RFC 4632](http://tools.ietf.org/html/rfc4632)和[RFC 4291](http://tools.ietf.org/html/rfc4291)。
本函數會返回IP地址和該IP所在的網絡和掩碼。例如,ParseCIDR("192.168.100.1/16")會返回IP地址192.168.100.1和IP網絡192.168.0.0/16。
### func (\*IPNet) [Contains](https://github.com/golang/go/blob/master/src/net/ip.go#L456 "View Source")
```
func (n *IPNet) Contains(ip IP) bool
```
Contains報告該網絡是否包含地址ip。
### func (\*IPNet) [Network](https://github.com/golang/go/blob/master/src/net/ip.go#L474 "View Source")
```
func (n *IPNet) Network() string
```
Network返回網絡類型名:"ip+net",注意該類型名是不合法的。
### func (\*IPNet) [String](https://github.com/golang/go/blob/master/src/net/ip.go#L482 "View Source")
```
func (n *IPNet) String() string
```
String返回n的CIDR表示,如"192.168.100.1/24"或"2001:DB8::/48",參見[RFC 4632](http://tools.ietf.org/html/rfc4632)和[RFC 4291](http://tools.ietf.org/html/rfc4291)。如果n的Mask字段不是規范格式,它會返回一個包含n.IP.String()、斜線、n.Mask.String()(此時表示為無標點十六進制格式)的字符串,如"192.168.100.1/c000ff00"。
## type [Addr](https://github.com/golang/go/blob/master/src/net/net.go#L54 "View Source")
```
type Addr interface {
Network() string // 網絡名
String() string // 字符串格式的地址
}
```
Addr代表一個網絡終端地址。
## type [Conn](https://github.com/golang/go/blob/master/src/net/net.go#L62 "View Source")
```
type Conn interface {
// Read從連接中讀取數據
// Read方法可能會在超過某個固定時間限制后超時返回錯誤,該錯誤的Timeout()方法返回真
Read(b []byte) (n int, err error)
// Write從連接中寫入數據
// Write方法可能會在超過某個固定時間限制后超時返回錯誤,該錯誤的Timeout()方法返回真
Write(b []byte) (n int, err error)
// Close方法關閉該連接
// 并會導致任何阻塞中的Read或Write方法不再阻塞并返回錯誤
Close() error
// 返回本地網絡地址
LocalAddr() Addr
// 返回遠端網絡地址
RemoteAddr() Addr
// 設定該連接的讀寫deadline,等價于同時調用SetReadDeadline和SetWriteDeadline
// deadline是一個絕對時間,超過該時間后I/O操作就會直接因超時失敗返回而不會阻塞
// deadline對之后的所有I/O操作都起效,而不僅僅是下一次的讀或寫操作
// 參數t為零值表示不設置期限
SetDeadline(t time.Time) error
// 設定該連接的讀操作deadline,參數t為零值表示不設置期限
SetReadDeadline(t time.Time) error
// 設定該連接的寫操作deadline,參數t為零值表示不設置期限
// 即使寫入超時,返回值n也可能>0,說明成功寫入了部分數據
SetWriteDeadline(t time.Time) error
}
```
Conn接口代表通用的面向流的網絡連接。多個線程可能會同時調用同一個Conn的方法。
### func [Dial](https://github.com/golang/go/blob/master/src/net/dial.go#L142 "View Source")
```
func Dial(network, address string) (Conn, error)
```
在網絡network上連接地址address,并返回一個Conn接口。可用的網絡類型有:
"tcp"、"tcp4"、"tcp6"、"udp"、"udp4"、"udp6"、"ip"、"ip4"、"ip6"、"unix"、"unixgram"、"unixpacket"
對TCP和UDP網絡,地址格式是host:port或[host]:port,參見函數JoinHostPort和SplitHostPort。
```
Dial("tcp", "12.34.56.78:80")
Dial("tcp", "google.com:http")
Dial("tcp", "[2001:db8::1]:http")
Dial("tcp", "[fe80::1%lo0]:80")
```
對IP網絡,network必須是"ip"、"ip4"、"ip6"后跟冒號和協議號或者協議名,地址必須是IP地址字面值。
```
Dial("ip4:1", "127.0.0.1")
Dial("ip6:ospf", "::1")
```
對Unix網絡,地址必須是文件系統路徑。
### func [DialTimeout](https://github.com/golang/go/blob/master/src/net/dial.go#L149 "View Source")
```
func DialTimeout(network, address string, timeout time.Duration) (Conn, error)
```
DialTimeout類似Dial但采用了超時。timeout參數如果必要可包含名稱解析。
### func [Pipe](https://github.com/golang/go/blob/master/src/net/pipe.go#L18 "View Source")
```
func Pipe() (Conn, Conn)
```
Pipe創建一個內存中的同步、全雙工網絡連接。連接的兩端都實現了Conn接口。一端的讀取對應另一端的寫入,直接將數據在兩端之間作拷貝;沒有內部緩沖。
## type [PacketConn](https://github.com/golang/go/blob/master/src/net/net.go#L218 "View Source")
```
type PacketConn interface {
// ReadFrom方法從連接讀取一個數據包,并將有效信息寫入b
// ReadFrom方法可能會在超過某個固定時間限制后超時返回錯誤,該錯誤的Timeout()方法返回真
// 返回寫入的字節數和該數據包的來源地址
ReadFrom(b []byte) (n int, addr Addr, err error)
// WriteTo方法將有效數據b寫入一個數據包發送給addr
// WriteTo方法可能會在超過某個固定時間限制后超時返回錯誤,該錯誤的Timeout()方法返回真
// 在面向數據包的連接中,寫入超時非常罕見
WriteTo(b []byte, addr Addr) (n int, err error)
// Close方法關閉該連接
// 會導致任何阻塞中的ReadFrom或WriteTo方法不再阻塞并返回錯誤
Close() error
// 返回本地網絡地址
LocalAddr() Addr
// 設定該連接的讀寫deadline
SetDeadline(t time.Time) error
// 設定該連接的讀操作deadline,參數t為零值表示不設置期限
// 如果時間到達deadline,讀操作就會直接因超時失敗返回而不會阻塞
SetReadDeadline(t time.Time) error
// 設定該連接的寫操作deadline,參數t為零值表示不設置期限
// 如果時間到達deadline,寫操作就會直接因超時失敗返回而不會阻塞
// 即使寫入超時,返回值n也可能>0,說明成功寫入了部分數據
SetWriteDeadline(t time.Time) error
}
```
PacketConn接口代表通用的面向數據包的網絡連接。多個線程可能會同時調用同一個Conn的方法。
### func [ListenPacket](https://github.com/golang/go/blob/master/src/net/dial.go#L285 "View Source")
```
func ListenPacket(net, laddr string) (PacketConn, error)
```
ListenPacket函數監聽本地網絡地址laddr。網絡類型net必須是面向數據包的網絡類型:
"ip"、"ip4"、"ip6"、"udp"、"udp4"、"udp6"、或"unixgram"。laddr的格式參見Dial函數。
## type [Dialer](https://github.com/golang/go/blob/master/src/net/dial.go#L17 "View Source")
```
type Dialer struct {
// Timeout是dial操作等待連接建立的最大時長,默認值代表沒有超時。
// 如果Deadline字段也被設置了,dial操作也可能更早失敗。
// 不管有沒有設置超時,操作系統都可能強制執行它的超時設置。
// 例如,TCP(系統)超時一般在3分鐘左右。
Timeout time.Duration
// Deadline是一個具體的時間點期限,超過該期限后,dial操作就會失敗。
// 如果Timeout字段也被設置了,dial操作也可能更早失敗。
// 零值表示沒有期限,即遵守操作系統的超時設置。
Deadline time.Time
// LocalAddr是dial一個地址時使用的本地地址。
// 該地址必須是與dial的網絡相容的類型。
// 如果為nil,將會自動選擇一個本地地址。
LocalAddr Addr
// DualStack允許單次dial操作在網絡類型為"tcp",
// 且目的地是一個主機名的DNS記錄具有多個地址時,
// 嘗試建立多個IPv4和IPv6連接,并返回第一個建立的連接。
DualStack bool
// KeepAlive指定一個活動的網絡連接的生命周期;如果為0,會禁止keep-alive。
// 不支持keep-alive的網絡連接會忽略本字段。
KeepAlive time.Duration
}
```
Dialer類型包含與某個地址建立連接時的參數。
每一個字段的零值都等價于沒有該字段。因此調用Dialer零值的Dial方法等價于調用Dial函數。
### func (\*Dialer) [Dial](https://github.com/golang/go/blob/master/src/net/dial.go#L158 "View Source")
```
func (d *Dialer) Dial(network, address string) (Conn, error)
```
Dial在指定的網絡上連接指定的地址。參見Dial函數獲取網絡和地址參數的描述。
## type [Listener](https://github.com/golang/go/blob/master/src/net/net.go#L266 "View Source")
```
type Listener interface {
// Addr返回該接口的網絡地址
Addr() Addr
// Accept等待并返回下一個連接到該接口的連接
Accept() (c Conn, err error)
// Close關閉該接口,并使任何阻塞的Accept操作都會不再阻塞并返回錯誤。
Close() error
}
```
Listener是一個用于面向流的網絡協議的公用的網絡監聽器接口。多個線程可能會同時調用一個Listener的方法。
Example
```
// Listen on TCP port 2000 on all interfaces.
l, err := net.Listen("tcp", ":2000")
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
// Wait for a connection.
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go func(c net.Conn) {
// Echo all incoming data.
io.Copy(c, c)
// Shut down the connection.
c.Close()
}(conn)
}
```
### func [Listen](https://github.com/golang/go/blob/master/src/net/dial.go#L261 "View Source")
```
func Listen(net, laddr string) (Listener, error)
```
返回在一個本地網絡地址laddr上監聽的Listener。網絡類型參數net必須是面向流的網絡:
"tcp"、"tcp4"、"tcp6"、"unix"或"unixpacket"。參見Dial函數獲取laddr的語法。
## type [IPAddr](https://github.com/golang/go/blob/master/src/net/iprawsock.go#L8 "View Source")
```
type IPAddr struct {
IP IP
Zone string // IPv6范圍尋址域
}
```
IPAddr代表一個IP終端的地址。
### func [ResolveIPAddr](https://github.com/golang/go/blob/master/src/net/iprawsock.go#L36 "View Source")
```
func ResolveIPAddr(net, addr string) (*IPAddr, error)
```
ResolveIPAddr將addr作為一個格式為"host"或"ipv6-host%zone"的IP地址來解析。?函數會在參數net指定的網絡類型上解析,net必須是"ip"、"ip4"或"ip6"。
### func (\*IPAddr) [Network](https://github.com/golang/go/blob/master/src/net/iprawsock.go#L14 "View Source")
```
func (a *IPAddr) Network() string
```
Network返回地址的網絡類型:"ip"。
### func (\*IPAddr) [String](https://github.com/golang/go/blob/master/src/net/iprawsock.go#L16 "View Source")
```
func (a *IPAddr) String() string
```
## type [TCPAddr](https://github.com/golang/go/blob/master/src/net/tcpsock.go#L8 "View Source")
```
type TCPAddr struct {
IP IP
Port int
Zone string // IPv6范圍尋址域
}
```
TCPAddr代表一個TCP終端地址。
### func [ResolveTCPAddr](https://github.com/golang/go/blob/master/src/net/tcpsock.go#L41 "View Source")
```
func ResolveTCPAddr(net, addr string) (*TCPAddr, error)
```
ResolveTCPAddr將addr作為TCP地址解析并返回。參數addr格式為"host:port"或"[ipv6-host%zone]:port",解析得到網絡名和端口名;net必須是"tcp"、"tcp4"或"tcp6"。
IPv6地址字面值/名稱必須用方括號包起來,如"[::1]:80"、"[ipv6-host]:http"或"[ipv6-host%zone]:80"。
### func (\*TCPAddr) [Network](https://github.com/golang/go/blob/master/src/net/tcpsock.go#L15 "View Source")
```
func (a *TCPAddr) Network() string
```
返回地址的網絡類型,"tcp"。
### func (\*TCPAddr) [String](https://github.com/golang/go/blob/master/src/net/tcpsock.go#L17 "View Source")
```
func (a *TCPAddr) String() string
```
## type [UDPAddr](https://github.com/golang/go/blob/master/src/net/udpsock.go#L8 "View Source")
```
type UDPAddr struct {
IP IP
Port int
Zone string // IPv6范圍尋址域
}
```
UDPAddr代表一個UDP終端地址。
### func [ResolveUDPAddr](https://github.com/golang/go/blob/master/src/net/udpsock.go#L41 "View Source")
```
func ResolveUDPAddr(net, addr string) (*UDPAddr, error)
```
ResolveTCPAddr將addr作為TCP地址解析并返回。參數addr格式為"host:port"或"[ipv6-host%zone]:port",解析得到網絡名和端口名;net必須是"udp"、"udp4"或"udp6"。
IPv6地址字面值/名稱必須用方括號包起來,如"[::1]:80"、"[ipv6-host]:http"或"[ipv6-host%zone]:80"。
### func (\*UDPAddr) [Network](https://github.com/golang/go/blob/master/src/net/udpsock.go#L15 "View Source")
```
func (a *UDPAddr) Network() string
```
返回地址的網絡類型,"udp"。
### func (\*UDPAddr) [String](https://github.com/golang/go/blob/master/src/net/udpsock.go#L17 "View Source")
```
func (a *UDPAddr) String() string
```
## type [UnixAddr](https://github.com/golang/go/blob/master/src/net/unixsock.go#L8 "View Source")
```
type UnixAddr struct {
Name string
Net string
}
```
UnixAddr代表一個Unix域socket終端地址。
### func [ResolveUnixAddr](https://github.com/golang/go/blob/master/src/net/unixsock.go#L36 "View Source")
```
func ResolveUnixAddr(net, addr string) (*UnixAddr, error)
```
ResolveUnixAddr將addr作為Unix域socket地址解析,參數net指定網絡類型:"unix"、"unixgram"或"unixpacket"。
### func (\*UnixAddr) [Network](https://github.com/golang/go/blob/master/src/net/unixsock.go#L15 "View Source")
```
func (a *UnixAddr) Network() string
```
返回地址的網絡類型,"unix","unixgram"或"unixpacket"。
### func (\*UnixAddr) [String](https://github.com/golang/go/blob/master/src/net/unixsock.go#L19 "View Source")
```
func (a *UnixAddr) String() string
```
## type [IPConn](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L62 "View Source")
```
type IPConn struct {
// 內含隱藏或非導出字段
}
```
IPConn類型代表IP網絡連接,實現了Conn和PacketConn接口。
### func [DialIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L184 "View Source")
```
func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error)
```
DialIP在網絡協議netProto上連接本地地址laddr和遠端地址raddr,netProto必須是"ip"、"ip4"或"ip6"后跟冒號和協議名或協議號。
### func [ListenIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L212 "View Source")
```
func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error)
```
ListenIP創建一個接收目的地是本地地址laddr的IP數據包的網絡連接,返回的\*IPConn的ReadFrom和WriteTo方法可以用來發送和接收IP數據包。(每個包都可獲取來源址或者設置目標地址)
### func (\*IPConn) [LocalAddr](https://github.com/golang/go/blob/master/src/net/net.go#L142 "View Source")
```
func (c *IPConn) LocalAddr() Addr
```
LocalAddr返回本地網絡地址
### func (\*IPConn) [RemoteAddr](https://github.com/golang/go/blob/master/src/net/net.go#L150 "View Source")
```
func (c *IPConn) RemoteAddr() Addr
```
RemoteAddr返回遠端網絡地址
### func (\*IPConn) [SetReadBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L183 "View Source")
```
func (c *IPConn) SetReadBuffer(bytes int) error
```
SetReadBuffer設置該連接的系統接收緩沖
### func (\*IPConn) [SetWriteBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L192 "View Source")
```
func (c *IPConn) SetWriteBuffer(bytes int) error
```
SetWriteBuffer設置該連接的系統發送緩沖
### func (\*IPConn) [SetDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L158 "View Source")
```
func (c *IPConn) SetDeadline(t time.Time) error
```
SetDeadline設置讀寫操作絕對期限,實現了Conn接口的SetDeadline方法
### func (\*IPConn) [SetReadDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L166 "View Source")
```
func (c *IPConn) SetReadDeadline(t time.Time) error
```
SetReadDeadline設置讀操作絕對期限,實現了Conn接口的SetReadDeadline方法
### func (\*IPConn) [SetWriteDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L174 "View Source")
```
func (c *IPConn) SetWriteDeadline(t time.Time) error
```
SetWriteDeadline設置寫操作絕對期限,實現了Conn接口的SetWriteDeadline方法
### func (\*IPConn) [Read](https://github.com/golang/go/blob/master/src/net/net.go#L118 "View Source")
```
func (c *IPConn) Read(b []byte) (int, error)
```
Read實現Conn接口Read方法
### func (\*IPConn) [ReadFrom](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L98 "View Source")
```
func (c *IPConn) ReadFrom(b []byte) (int, Addr, error)
```
ReadFrom實現PacketConn接口ReadFrom方法。注意本方法有bug,應避免使用。
### func (\*IPConn) [ReadFromIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L75 "View Source")
```
func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error)
```
ReadFromIP從c讀取一個IP數據包,將有效負載拷貝到b,返回拷貝字節數和數據包來源地址。
ReadFromIP方法會在超過一個固定的時間點之后超時,并返回一個錯誤。注意本方法有bug,應避免使用。
### func (\*IPConn) [ReadMsgIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L110 "View Source")
```
func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error)
```
ReadMsgIP從c讀取一個數據包,將有效負載拷貝進b,相關的帶外數據拷貝進oob,返回拷貝進b的字節數,拷貝進oob的字節數,數據包的flag,數據包來源地址和可能的錯誤。
### func (\*IPConn) [Write](https://github.com/golang/go/blob/master/src/net/net.go#L126 "View Source")
```
func (c *IPConn) Write(b []byte) (int, error)
```
Write實現Conn接口Write方法
### func (\*IPConn) [WriteTo](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L150 "View Source")
```
func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error)
```
WriteTo實現PacketConn接口WriteTo方法
### func (\*IPConn) [WriteToIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L132 "View Source")
```
func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error)
```
WriteToIP通過c向地址addr發送一個數據包,b為包的有效負載,返回寫入的字節。
WriteToIP方法會在超過一個固定的時間點之后超時,并返回一個錯誤。在面向數據包的連接上,寫入超時是十分罕見的。
### func (\*IPConn) [WriteMsgIP](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L164 "View Source")
```
func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error)
```
WriteMsgIP通過c向地址addr發送一個數據包,b和oob分別為包有效負載和對應的帶外數據,返回寫入的字節數(包數據、帶外數據)和可能的錯誤。
### func (\*IPConn) [Close](https://github.com/golang/go/blob/master/src/net/net.go#L134 "View Source")
```
func (c *IPConn) Close() error
```
Close關閉連接
### func (\*IPConn) [File](https://github.com/golang/go/blob/master/src/net/net.go#L206 "View Source")
```
func (c *IPConn) File() (f *os.File, err error)
```
File方法設置下層的os.File為阻塞模式并返回其副本。
使用者有責任在用完后關閉f。關閉c不影響f,關閉f也不影響c。返回的os.File類型文件描述符和原本的網絡連接是不同的。試圖使用該副本修改本體的屬性可能會(也可能不會)得到期望的效果。
## type [TCPConn](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L57 "View Source")
```
type TCPConn struct {
// 內含隱藏或非導出字段
}
```
TCPConn代表一個TCP網絡連接,實現了Conn接口。
### func [DialTCP](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L143 "View Source")
```
func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error)
```
DialTCP在網絡協議net上連接本地地址laddr和遠端地址raddr。net必須是"tcp"、"tcp4"、"tcp6";如果laddr不是nil,將使用它作為本地地址,否則自動選擇一個本地地址。
### func (\*TCPConn) [LocalAddr](https://github.com/golang/go/blob/master/src/net/net.go#L142 "View Source")
```
func (c *TCPConn) LocalAddr() Addr
```
LocalAddr返回本地網絡地址
### func (\*TCPConn) [RemoteAddr](https://github.com/golang/go/blob/master/src/net/net.go#L150 "View Source")
```
func (c *TCPConn) RemoteAddr() Addr
```
RemoteAddr返回遠端網絡地址
### func (\*TCPConn) [SetReadBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L183 "View Source")
```
func (c *TCPConn) SetReadBuffer(bytes int) error
```
SetReadBuffer設置該連接的系統接收緩沖
### func (\*TCPConn) [SetWriteBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L192 "View Source")
```
func (c *TCPConn) SetWriteBuffer(bytes int) error
```
SetWriteBuffer設置該連接的系統發送緩沖
### func (\*TCPConn) [SetDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L158 "View Source")
```
func (c *TCPConn) SetDeadline(t time.Time) error
```
SetDeadline設置讀寫操作期限,實現了Conn接口的SetDeadline方法
### func (\*TCPConn) [SetReadDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L166 "View Source")
```
func (c *TCPConn) SetReadDeadline(t time.Time) error
```
SetReadDeadline設置讀操作期限,實現了Conn接口的SetReadDeadline方法
### func (\*TCPConn) [SetWriteDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L174 "View Source")
```
func (c *TCPConn) SetWriteDeadline(t time.Time) error
```
SetWriteDeadline設置寫操作期限,實現了Conn接口的SetWriteDeadline方法
### func (\*TCPConn) [SetKeepAlive](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L114 "View Source")
```
func (c *TCPConn) SetKeepAlive(keepalive bool) error
```
SetKeepAlive設置操作系統是否應該在該連接中發送keepalive信息
### func (\*TCPConn) [SetKeepAlivePeriod](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L122 "View Source")
```
func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error
```
SetKeepAlivePeriod設置keepalive的周期,超出會斷開
### func (\*TCPConn) [SetLinger](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L105 "View Source")
```
func (c *TCPConn) SetLinger(sec int) error
```
SetLinger設定當連接中仍有數據等待發送或接受時的Close方法的行為。
如果sec < 0(默認),Close方法立即返回,操作系統停止后臺數據發送;如果 sec == 0,Close立刻返回,操作系統丟棄任何未發送或未接收的數據;如果sec > 0,Close方法阻塞最多sec秒,等待數據發送或者接收,在一些操作系統中,在超時后,任何未發送的數據會被丟棄。
### func (\*TCPConn) [SetNoDelay](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L133 "View Source")
```
func (c *TCPConn) SetNoDelay(noDelay bool) error
```
SetNoDelay設定操作系統是否應該延遲數據包傳遞,以便發送更少的數據包(Nagle's算法)。默認為真,即數據應該在Write方法后立刻發送。
### func (\*TCPConn) [Read](https://github.com/golang/go/blob/master/src/net/net.go#L118 "View Source")
```
func (c *TCPConn) Read(b []byte) (int, error)
```
Read實現了Conn接口Read方法
### func (\*TCPConn) [Write](https://github.com/golang/go/blob/master/src/net/net.go#L126 "View Source")
```
func (c *TCPConn) Write(b []byte) (int, error)
```
Write實現了Conn接口Write方法
### func (\*TCPConn) [ReadFrom](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L68 "View Source")
```
func (c *TCPConn) ReadFrom(r io.Reader) (int64, error)
```
ReadFrom實現了io.ReaderFrom接口的ReadFrom方法
### func (\*TCPConn) [Close](https://github.com/golang/go/blob/master/src/net/net.go#L134 "View Source")
```
func (c *TCPConn) Close() error
```
Close關閉連接
### func (\*TCPConn) [CloseRead](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L77 "View Source")
```
func (c *TCPConn) CloseRead() error
```
CloseRead關閉TCP連接的讀取側(以后不能讀取),應盡量使用Close方法。
### func (\*TCPConn) [CloseWrite](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L86 "View Source")
```
func (c *TCPConn) CloseWrite() error
```
CloseWrite關閉TCP連接的寫入側(以后不能寫入),應盡量使用Close方法。
### func (\*TCPConn) [File](https://github.com/golang/go/blob/master/src/net/net.go#L206 "View Source")
```
func (c *TCPConn) File() (f *os.File, err error)
```
File方法設置下層的os.File為阻塞模式并返回其副本。
使用者有責任在用完后關閉f。關閉c不影響f,關閉f也不影響c。返回的os.File類型文件描述符和原本的網絡連接是不同的。試圖使用該副本修改本體的屬性可能會(也可能不會)得到期望的效果。
## type [UDPConn](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L50 "View Source")
```
type UDPConn struct {
// 內含隱藏或非導出字段
}
```
UDPConn代表一個UDP網絡連接,實現了Conn和PacketConn接口。
### func [DialUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L165 "View Source")
```
func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error)
```
DialTCP在網絡協議net上連接本地地址laddr和遠端地址raddr。net必須是"udp"、"udp4"、"udp6";如果laddr不是nil,將使用它作為本地地址,否則自動選擇一個本地地址。
### func [ListenUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L192 "View Source")
```
func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error)
```
ListenUDP創建一個接收目的地是本地地址laddr的UDP數據包的網絡連接。net必須是"udp"、"udp4"、"udp6";如果laddr端口為0,函數將選擇一個當前可用的端口,可以用Listener的Addr方法獲得該端口。返回的\*UDPConn的ReadFrom和WriteTo方法可以用來發送和接收UDP數據包(每個包都可獲得來源地址或設置目標地址)。
### func [ListenMulticastUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L212 "View Source")
```
func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error)
```
ListenMulticastUDP接收目的地是ifi接口上的組地址gaddr的UDP數據包。它指定了使用的接口,如果ifi是nil,將使用默認接口。
### func (\*UDPConn) [LocalAddr](https://github.com/golang/go/blob/master/src/net/net.go#L142 "View Source")
```
func (c *UDPConn) LocalAddr() Addr
```
LocalAddr返回本地網絡地址
### func (\*UDPConn) [RemoteAddr](https://github.com/golang/go/blob/master/src/net/net.go#L150 "View Source")
```
func (c *UDPConn) RemoteAddr() Addr
```
RemoteAddr返回遠端網絡地址
### func (\*UDPConn) [SetReadBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L183 "View Source")
```
func (c *UDPConn) SetReadBuffer(bytes int) error
```
SetReadBuffer設置該連接的系統接收緩沖
### func (\*UDPConn) [SetWriteBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L192 "View Source")
```
func (c *UDPConn) SetWriteBuffer(bytes int) error
```
SetWriteBuffer設置該連接的系統發送緩沖
### func (\*UDPConn) [SetDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L158 "View Source")
```
func (c *UDPConn) SetDeadline(t time.Time) error
```
SetDeadline設置讀寫操作期限,實現了Conn接口的SetDeadline方法
### func (\*UDPConn) [SetReadDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L166 "View Source")
```
func (c *UDPConn) SetReadDeadline(t time.Time) error
```
SetReadDeadline設置讀操作期限,實現了Conn接口的SetReadDeadline方法
### func (\*UDPConn) [SetWriteDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L174 "View Source")
```
func (c *UDPConn) SetWriteDeadline(t time.Time) error
```
SetWriteDeadline設置寫操作期限,實現了Conn接口的SetWriteDeadline方法
### func (\*UDPConn) [Read](https://github.com/golang/go/blob/master/src/net/net.go#L118 "View Source")
```
func (c *UDPConn) Read(b []byte) (int, error)
```
Read實現Conn接口Read方法
### func (\*UDPConn) [ReadFrom](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L78 "View Source")
```
func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error)
```
ReadFrom實現PacketConn接口ReadFrom方法
### func (\*UDPConn) [ReadFromUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L63 "View Source")
```
func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error)
```
ReadFromUDP從c讀取一個UDP數據包,將有效負載拷貝到b,返回拷貝字節數和數據包來源地址。
ReadFromUDP方法會在超過一個固定的時間點之后超時,并返回一個錯誤。
### func (\*UDPConn) [ReadMsgUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L91 "View Source")
```
func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error)
```
ReadMsgUDP從c讀取一個數據包,將有效負載拷貝進b,相關的帶外數據拷貝進oob,返回拷貝進b的字節數,拷貝進oob的字節數,數據包的flag,數據包來源地址和可能的錯誤。
### func (\*UDPConn) [Write](https://github.com/golang/go/blob/master/src/net/net.go#L126 "View Source")
```
func (c *UDPConn) Write(b []byte) (int, error)
```
Write實現Conn接口Write方法
### func (\*UDPConn) [WriteTo](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L131 "View Source")
```
func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error)
```
WriteTo實現PacketConn接口WriteTo方法
### func (\*UDPConn) [WriteToUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L113 "View Source")
```
func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error)
```
WriteToUDP通過c向地址addr發送一個數據包,b為包的有效負載,返回寫入的字節。
WriteToUDP方法會在超過一個固定的時間點之后超時,并返回一個錯誤。在面向數據包的連接上,寫入超時是十分罕見的。
### func (\*UDPConn) [WriteMsgUDP](https://github.com/golang/go/blob/master/src/net/udpsock_posix.go#L145 "View Source")
```
func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error)
```
WriteMsgUDP通過c向地址addr發送一個數據包,b和oob分別為包有效負載和對應的帶外數據,返回寫入的字節數(包數據、帶外數據)和可能的錯誤。
### func (\*UDPConn) [Close](https://github.com/golang/go/blob/master/src/net/net.go#L134 "View Source")
```
func (c *UDPConn) Close() error
```
Close關閉連接
### func (\*UDPConn) [File](https://github.com/golang/go/blob/master/src/net/net.go#L206 "View Source")
```
func (c *UDPConn) File() (f *os.File, err error)
```
File方法設置下層的os.File為阻塞模式并返回其副本。
使用者有責任在用完后關閉f。關閉c不影響f,關閉f也不影響c。返回的os.File類型文件描述符和原本的網絡連接是不同的。試圖使用該副本修改本體的屬性可能會(也可能不會)得到期望的效果。
## type [UnixConn](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L110 "View Source")
```
type UnixConn struct {
// 內含隱藏或非導出字段
}
```
UnixConn代表Unix域socket連接,實現了Conn和PacketConn接口。
### func [DialUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L240 "View Source")
```
func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error)
```
DialUnix在網絡協議net上連接本地地址laddr和遠端地址raddr。net必須是"unix"、"unixgram"、"unixpacket",如果laddr不是nil將使用它作為本地地址,否則自動選擇一個本地地址。
### func [ListenUnixgram](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L359 "View Source")
```
func ListenUnixgram(net string, laddr *UnixAddr) (*UnixConn, error)
```
ListenUnixgram接收目的地是本地地址laddr的Unix datagram網絡連接。net必須是"unixgram",返回的\*UnixConn的ReadFrom和WriteTo方法可以用來發送和接收數據包(每個包都可獲取來源址或者設置目標地址)。
### func (\*UnixConn) [LocalAddr](https://github.com/golang/go/blob/master/src/net/net.go#L142 "View Source")
```
func (c *UnixConn) LocalAddr() Addr
```
LocalAddr返回本地網絡地址
### func (\*UnixConn) [RemoteAddr](https://github.com/golang/go/blob/master/src/net/net.go#L150 "View Source")
```
func (c *UnixConn) RemoteAddr() Addr
```
RemoteAddr返回遠端網絡地址
### func (\*UnixConn) [SetReadBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L183 "View Source")
```
func (c *UnixConn) SetReadBuffer(bytes int) error
```
SetReadBuffer設置該連接的系統接收緩沖
### func (\*UnixConn) [SetWriteBuffer](https://github.com/golang/go/blob/master/src/net/net.go#L192 "View Source")
```
func (c *UnixConn) SetWriteBuffer(bytes int) error
```
SetWriteBuffer設置該連接的系統發送緩沖
### func (\*UnixConn) [SetDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L158 "View Source")
```
func (c *UnixConn) SetDeadline(t time.Time) error
```
SetDeadline設置讀寫操作期限,實現了Conn接口的SetDeadline方法
### func (\*UnixConn) [SetReadDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L166 "View Source")
```
func (c *UnixConn) SetReadDeadline(t time.Time) error
```
SetReadDeadline設置讀操作期限,實現了Conn接口的SetReadDeadline方法
### func (\*UnixConn) [SetWriteDeadline](https://github.com/golang/go/blob/master/src/net/net.go#L174 "View Source")
```
func (c *UnixConn) SetWriteDeadline(t time.Time) error
```
SetWriteDeadline設置寫操作期限,實現了Conn接口的SetWriteDeadline方法
### func (\*UnixConn) [Read](https://github.com/golang/go/blob/master/src/net/net.go#L118 "View Source")
```
func (c *UnixConn) Read(b []byte) (int, error)
```
Read實現了Conn接口Read方法
### func (\*UnixConn) [ReadFrom](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L138 "View Source")
```
func (c *UnixConn) ReadFrom(b []byte) (int, Addr, error)
```
ReadFrom實現PacketConn接口ReadFrom方法
### func (\*UnixConn) [ReadFromUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L123 "View Source")
```
func (c *UnixConn) ReadFromUnix(b []byte) (n int, addr *UnixAddr, err error)
```
ReadFromUnix從c讀取一個UDP數據包,將有效負載拷貝到b,返回拷貝字節數和數據包來源地址。
ReadFromUnix方法會在超過一個固定的時間點之后超時,并返回一個錯誤。
### func (\*UnixConn) [ReadMsgUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L150 "View Source")
```
func (c *UnixConn) ReadMsgUnix(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error)
```
ReadMsgUnix從c讀取一個數據包,將有效負載拷貝進b,相關的帶外數據拷貝進oob,返回拷貝進b的字節數,拷貝進oob的字節數,數據包的flag,數據包來源地址和可能的錯誤。
### func (\*UnixConn) [Write](https://github.com/golang/go/blob/master/src/net/net.go#L126 "View Source")
```
func (c *UnixConn) Write(b []byte) (int, error)
```
Write實現了Conn接口Write方法
### func (\*UnixConn) [WriteTo](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L188 "View Source")
```
func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err error)
```
WriteTo實現PacketConn接口WriteTo方法
### func (\*UnixConn) [WriteToUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L170 "View Source")
```
func (c *UnixConn) WriteToUnix(b []byte, addr *UnixAddr) (n int, err error)
```
WriteToUnix通過c向地址addr發送一個數據包,b為包的有效負載,返回寫入的字節。
WriteToUnix方法會在超過一個固定的時間點之后超時,并返回一個錯誤。在面向數據包的連接上,寫入超時是十分罕見的。
### func (\*UnixConn) [WriteMsgUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L202 "View Source")
```
func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err error)
```
WriteMsgUnix通過c向地址addr發送一個數據包,b和oob分別為包有效負載和對應的帶外數據,返回寫入的字節數(包數據、帶外數據)和可能的錯誤。
### func (\*UnixConn) [Close](https://github.com/golang/go/blob/master/src/net/net.go#L134 "View Source")
```
func (c *UnixConn) Close() error
```
Close關閉連接
### func (\*UnixConn) [CloseRead](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L221 "View Source")
```
func (c *UnixConn) CloseRead() error
```
CloseRead關閉TCP連接的讀取側(以后不能讀取),應盡量使用Close方法
### func (\*UnixConn) [CloseWrite](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L230 "View Source")
```
func (c *UnixConn) CloseWrite() error
```
CloseWrite關閉TCP連接的寫入側(以后不能寫入),應盡量使用Close方法
### func (\*UnixConn) [File](https://github.com/golang/go/blob/master/src/net/net.go#L206 "View Source")
```
func (c *UnixConn) File() (f *os.File, err error)
```
File方法設置下層的os.File為阻塞模式并返回其副本。
使用者有責任在用完后關閉f。關閉c不影響f,關閉f也不影響c。返回的os.File類型文件描述符和原本的網絡連接是不同的。試圖使用該副本修改本體的屬性可能會(也可能不會)得到期望的效果。
## type [TCPListener](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L224 "View Source")
```
type TCPListener struct {
// 內含隱藏或非導出字段
}
```
TCPListener代表一個TCP網絡的監聽者。使用者應盡量使用Listener接口而不是假設(網絡連接為)TCP。
### func [ListenTCP](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L285 "View Source")
```
func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error)
```
ListenTCP在本地TCP地址laddr上聲明并返回一個\*TCPListener,net參數必須是"tcp"、"tcp4"、"tcp6",如果laddr的端口字段為0,函數將選擇一個當前可用的端口,可以用Listener的Addr方法獲得該端口。
### func (\*TCPListener) [Addr](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L261 "View Source")
```
func (l *TCPListener) Addr() Addr
```
Addr返回l監聽的的網絡地址,一個\*TCPAddr。
### func (\*TCPListener) [SetDeadline](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L265 "View Source")
```
func (l *TCPListener) SetDeadline(t time.Time) error
```
設置監聽器執行的期限,t為Time零值則會關閉期限限制。
### func (\*TCPListener) [Accept](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L243 "View Source")
```
func (l *TCPListener) Accept() (Conn, error)
```
Accept用于實現Listener接口的Accept方法;他會等待下一個呼叫,并返回一個該呼叫的Conn接口。
### func (\*TCPListener) [AcceptTCP](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L230 "View Source")
```
func (l *TCPListener) AcceptTCP() (*TCPConn, error)
```
AcceptTCP接收下一個呼叫,并返回一個新的\*TCPConn。
### func (\*TCPListener) [Close](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L253 "View Source")
```
func (l *TCPListener) Close() error
```
Close停止監聽TCP地址,已經接收的連接不受影響。
### func (\*TCPListener) [File](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L279 "View Source")
```
func (l *TCPListener) File() (f *os.File, err error)
```
File方法返回下層的os.File的副本,并將該副本設置為阻塞模式。
使用者有責任在用完后關閉f。關閉c不影響f,關閉f也不影響c。返回的os.File類型文件描述符和原本的網絡連接是不同的。試圖使用該副本修改本體的屬性可能會(也可能不會)得到期望的效果。
## type [UnixListener](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L260 "View Source")
```
type UnixListener struct {
// 內含隱藏或非導出字段
}
```
UnixListener代表一個Unix域scoket的監聽者。使用者應盡量使用Listener接口而不是假設(網絡連接為)Unix域scoket。
### func [ListenUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L267 "View Source")
```
func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error)
```
ListenTCP在Unix域scoket地址laddr上聲明并返回一個\*UnixListener,net參數必須是"unix"或"unixpacket"。
### func (\*UnixListener) [Addr](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L335 "View Source")
```
func (l *UnixListener) Addr() Addr
```
Addr返回l的監聽的Unix域socket地址
### func (\*UnixListener) [SetDeadline](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L339 "View Source")
```
func (l *UnixListener) SetDeadline(t time.Time) (err error)
```
設置監聽器執行的期限,t為Time零值則會關閉期限限制
### func (\*UnixListener) [Accept](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L303 "View Source")
```
func (l *UnixListener) Accept() (c Conn, err error)
```
Accept用于實現Listener接口的Accept方法;他會等待下一個呼叫,并返回一個該呼叫的Conn接口。
### func (\*UnixListener) [AcceptUnix](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L285 "View Source")
```
func (l *UnixListener) AcceptUnix() (*UnixConn, error)
```
AcceptUnix接收下一個呼叫,并返回一個新的\*UnixConn。
### func (\*UnixListener) [Close](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L313 "View Source")
```
func (l *UnixListener) Close() error
```
Close停止監聽Unix域socket地址,已經接收的連接不受影響。
### func (\*UnixListener) [File](https://github.com/golang/go/blob/master/src/net/unixsock_posix.go#L353 "View Source")
```
func (l *UnixListener) File() (f *os.File, err error)
```
File方法返回下層的os.File的副本,并將該副本設置為阻塞模式。
使用者有責任在用完后關閉f。關閉c不影響f,關閉f也不影響c。返回的os.File類型文件描述符和原本的網絡連接是不同的。試圖使用該副本修改本體的屬性可能會(也可能不會)得到期望的效果。
## func [FileConn](https://github.com/golang/go/blob/master/src/net/file_unix.go#L82 "View Source")
```
func FileConn(f *os.File) (c Conn, err error)
```
FileConn返回一個下層為文件f的網絡連接的拷貝。調用者有責任在結束程序前關閉f。關閉c不會影響f,關閉f也不會影響c。本函數與各種實現了Conn接口的類型的File方法是對應的。
## func [FilePacketConn](https://github.com/golang/go/blob/master/src/net/file_unix.go#L124 "View Source")
```
func FilePacketConn(f *os.File) (c PacketConn, err error)
```
FilePacketConn函數返回一個下層為文件f的數據包網絡連接的拷貝。調用者有責任在結束程序前關閉f。關閉c不會影響f,關閉f也不會影響c。本函數與各種實現了PacketConn接口的類型的File方法是對應的。
## func [FileListener](https://github.com/golang/go/blob/master/src/net/file_unix.go#L105 "View Source")
```
func FileListener(f *os.File) (l Listener, err error)
```
FileListener返回一個下層為文件f的網絡監聽器的拷貝。調用者有責任在使用結束后改變l。關閉l不會影響f,關閉f也不會影響l。本函數與各種實現了Listener接口的類型的File方法是對應的。
## type [MX](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L225 "View Source")
```
type MX struct {
Host string
Pref uint16
}
```
MX代表一條DNS MX記錄(郵件交換記錄),根據收信人的地址后綴來定位郵件服務器。
## type [NS](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L249 "View Source")
```
type NS struct {
Host string
}
```
NS代表一條DNS NS記錄(域名服務器記錄),指定該域名由哪個DNS服務器來進行解析。
## type [SRV](https://github.com/golang/go/blob/master/src/net/dnsclient.go#L166 "View Source")
```
type SRV struct {
Target string
Port uint16
Priority uint16
Weight uint16
}
```
SRV代表一條DNS SRV記錄(資源記錄),記錄某個服務由哪臺計算機提供。
## func [LookupPort](https://github.com/golang/go/blob/master/src/net/lookup.go#L93 "View Source")
```
func LookupPort(network, service string) (port int, err error)
```
LookupPort函數查詢指定網絡和服務的(默認)端口。
## func [LookupCNAME](https://github.com/golang/go/blob/master/src/net/lookup.go#L101 "View Source")
```
func LookupCNAME(name string) (cname string, err error)
```
LookupCNAME函數查詢name的規范DNS名(但該域名未必可以訪問)。如果調用者不關心規范名可以直接調用LookupHost或者LookupIP;這兩個函數都會在查詢時考慮到規范名。
## func [LookupHost](https://github.com/golang/go/blob/master/src/net/lookup.go#L24 "View Source")
```
func LookupHost(host string) (addrs []string, err error)
```
LookupHost函數查詢主機的網絡地址序列。
## func [LookupIP](https://github.com/golang/go/blob/master/src/net/lookup.go#L30 "View Source")
```
func LookupIP(host string) (addrs []IP, err error)
```
LookupIP函數查詢主機的ipv4和ipv6地址序列。
## func [LookupAddr](https://github.com/golang/go/blob/master/src/net/lookup.go#L135 "View Source")
```
func LookupAddr(addr string) (name []string, err error)
```
LookupAddr查詢某個地址,返回映射到該地址的主機名序列,本函數和LookupHost不互為反函數。
## func [LookupMX](https://github.com/golang/go/blob/master/src/net/lookup.go#L119 "View Source")
```
func LookupMX(name string) (mx []*MX, err error)
```
LookupMX函數返回指定主機的按Pref字段排好序的DNS MX記錄。
## func [LookupNS](https://github.com/golang/go/blob/master/src/net/lookup.go#L124 "View Source")
```
func LookupNS(name string) (ns []*NS, err error)
```
LookupNS函數返回指定主機的DNS NS記錄。
## func [LookupSRV](https://github.com/golang/go/blob/master/src/net/lookup.go#L114 "View Source")
```
func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err error)
```
LookupSRV函數嘗試執行指定服務、協議、主機的SRV查詢。協議proto為"tcp"?或"udp"。返回的記錄按Priority字段排序,同一優先度按Weight字段隨機排序。
LookupSRV函數按照[RFC 2782](http://tools.ietf.org/html/rfc2782)的規定構建用于查詢的DNS名。也就是說,它會查詢_service._proto.name。為了適應將服務的SRV記錄發布在非規范名下的情況,如果service和proto參數都是空字符串,函數會直接查詢name。
## func [LookupTXT](https://github.com/golang/go/blob/master/src/net/lookup.go#L129 "View Source")
```
func LookupTXT(name string) (txt []string, err error)
```
LookupTXT函數返回指定主機的DNS TXT記錄。
## Bugs
[?](https://github.com/golang/go/blob/master/src/net/iprawsock_posix.go#L14 "View Source")在任何POSIX平臺上,從"ip4"網絡使用ReadFrom或ReadFromIP方法讀取數據時,即使有足夠的空間,都可能不會返回完整的IPv4數據包,包括數據包的頭域。即使Read或ReadMsgIP方法可以返回完整的數據包,也有可能出現這種情況。因為對go 1的兼容性要求,這個情況無法被修正。因此,當必須獲取完整數據包時,建議你不要使用這兩個方法,請使用Read或ReadMsgIP代替。
[?](https://github.com/golang/go/blob/master/src/net/tcpsock_posix.go#L16 "View Source")在OpenBSD系統中,在"tcp"網絡監聽時不會同時監聽IPv4和IPv6連接。?因為該系統中IPv4通信不會導入IPv6套接字中。請使用兩個獨立的監聽,如果有必要的話。
- 庫
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe