net

网络IO接口,包括TCP/IP, UDP, SOCKET, DNS

1
import "net"

constants

1
2
3
4
const (
    IPv4len = 4
    IPv6len = 16
)

variables

functions

Addr

代表一个网络终端地址.

TCPAddr, UDPAddr, IPAddr, UnixAddr 都实现了该接口

1
2
3
4
type Addr interface {
    Network() string
    String() string
}

TCPAddr

function:

1
func ResolveTCPAddr(net, addr string) (*TCPAddr, error)

UDPAddr

function:

1
func ResolveUDPAddr(net, addr string) (*UDPAddr, error)

IPAddr

function:

1
func ResolveIPAddr(net, addr string) (*IPAddr, error)

UnixAddr

function:

1
func ResolveUnixAddr(net, addr string) (*UnixAddr, error)

Conn

该接口代表通用的面向流的网络连接.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
type Conn interface {
    Read()
    Write()
    Close()
    LocalAddr()
    RemoteAddr()
    SetDeadline()
    SetReadDeadline()
    setWriteDeadline()
}

function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// tcp: "tcp"、"tcp4"、"tcp6"、
// unix: "unix", "unixpacket"
// unix: "unixgramh"
// udp: "udp"、"udp4"、"udp6"、
// ip: "ip"、"ip4"、"ip6"、
func Dail(network, address string) (Conn, error)

func DialTimeout(network, address string, timeout time.Duration) (Conn, error)

func Pipe() (Conn, Conn)

PacketConn

该接口代表通用的面向数据包的网络连接.

1
2
3
4
5
6
7
8
9
type PacketConn interface {
    ReadDrom()
    WriteTo()
    Close()
    LocalAddr()
    SetDeadline()
    SetReadDeadline()
    SetWriteDeadline()
}

function:

1
2
3
4
// ip: "ip"、"ip4"、"ip6"、
// udp: "udp"、"udp4"、"udp6"、
// unix: "unixgram"
func ListenPacket(net, laddr string) (PacketConn, error)

Listener

通用的面向流的网络协议的公用的网络监听接口.

1
2
3
4
5
type Listener interface {
    Addr() addr
    Accept() (c Conn, err error)
    Close() error
}

function:

1
2
3
// tcp: "tcp"、"tcp4"、"tcp6"、
// unix: "unix", "unixpacket"
func Listen(net, laddr string) (Listener, error)

IPConn

实现了Conn和PacketConn接口.

function:

1
2
func DilIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error)
func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error)

method:

UDPConn

实现了Conn和PacketConn接口.

function:

1
2
func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error)
func ListenDUP(net string, laddr *UDPAddr) (*UDPConn, error)

method:

TCPConn

实现了Conn接口.

function:

1
func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error)

method:

TCPListener

function:

1
func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error)

method:

1
func (l *TCPListener) Accept() (Conn, error)

UnixConn

实现了Conn和PacketConn接口.

function:

1
2
func DialUnix(net string, laddr, raddr *UnixAddr) (*UnixConn, error)
func ListenUnixgram(net string, laddr *UnixAddr) (*UnixConn, error)

method:

UnixListener

function:

1
func ListenUnix(net string, laddr *UnixAddr) (*UnixListener, error)

method:

1
2
func (l *UnixListener) Accept() (c Conn, err error)
func (l *UnixListener) AcceptUnix() (*UnixConn, error)

net/http

http协议客户端和服务器的实现

1
import "net/http"

constants

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
const (
    MethodGet = "GET"
    MethodHead = "HEAD"
    MethodPost = "POST"
    MethodPut = "PUT"
    MethodPatch = "PATCH"
    MethodDelete = "DELETE"
    MethodConnect = "CONNECT"
    MethodOptions = "OPTIONS"
    MethodTrace = "TRACE"
)

variables

functions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// 在w的header中添加Set-Cookie头.
func SetCookie(w ResponseWriter, cookie *Cookie)

func Handle(pattern string, handler Handler)

// 注册一个handler和对应的pattern 到DefaultServeMux.
func HandlerFunc(pattern string, handler func(ResponseWriter, *Request))

// 为监听器收到的每个连接创建一个新的goroutine.
// goroutine 会读取请求并调用handler回复该请求.
func Serve(l net.Listener, handler Handler) error

// 监听tcp地址addr, 使用handler参数调用Serve函数处理连接.
// handler = nil 相当于DefaultServeMux
func ListenAndServe(addr string, handler Handler) error

File

1
2
3
4
5
6
7
type File interface {
    io.Closer
    io.Reader
    Readdir(count int) ([]os.FileInfo, error)
    Seek(offset int64, whence int) (int64, error)
    Stat() (os.FileInfo, error)
}

FileSystem

1
2
3
type FileSystem interface {
    Open(name string) (File, error)
}

Dir

1
type Dir string

method:

1
func (d Dir) Open(name string) (File, error)

Handler

实现Handler接口的对象可以被注册为http的服务函数

1
2
3
type Handler interface {
    ServeHTTP(ResponseWriter, *Request)
}

function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// 对每个请求回复"404 page not found"
func NotFoundHandler() Handler

// 对每个请求使用状态码code重定向到url.
func RedirectHandler(url string, code int) Handler

func TimeoutHandler(h Handler, dt time.Duration, msg string) Handler

// 将请求的URL.Path中的前缀prefix去除后再给h.
func StripPrefix(prefix string, h Handler) Handler

func FileServer(root FileSystem) Handler

HandlerFunc

将普通函数转换成http的服务函数

1
type HandlerFunc func(ResponseWriter, *Request)

method:

1
2
// ServeHTTP会调用f(w, r)
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request)

ServeMux

ServeMux是http请求的多路转接器.

1
type ServeMux struct {}

function:

1
2
// 创建并返回一个新的*ServeMux
func NewServeMux() *ServeMux

method:

1
2
3
4
5
6
7
8
//
func (mux *ServeMux) Handle(pattern string, handler Handler)

func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request))

func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string)

func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request)

Server

定义了运行http服务端的参数.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
type Server struct {
    Addr string
    Handler Handler
    ReadTimeout time.Duration
    WriteTimeout time.Duration
    MaxHeaderBytes int
    TLSConfig *tls.Config
    TLSNextProto map[string]func(*Server, *tls.Conn, Handler)
    ConnState func(net.Conn, ConnState)
    ErrorLog *log.Logger
}

method:

1
2
3
4
5
6
7
func (s *Server) SetKeepAlivesEnabled(v bool)

func (s *Server) Serve(l net.Listener) error

func (s *Server) ListenAndServe() error

func (s *Server) ListenAndServeTLS(certFile, keyFile string) error

Client

代表一个http客户端.

1
2
3
4
5
6
type Client struct {
    Transport RoundTripper
    CheckRedirect func(req *Request, via []*Request) error
    Jar CookieJar
    Timeout time.Duration
}

代表http的头部.

1
type Header map[string][]string

method:

1
2
3
func (h Header) Get(key string) string
func (h Header) Set(key, value string)
...

代表一个http回复的头部中SetCookie头的值,或http请求的头部里面的cookie值.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
type Cookie struct {
    Name string
    Value string
    Path string
    Domain string
    Expires time.Time
    RawExpires string
    MaxAge int
    Secure bool
    HttpOnly bool
    Raw string
    Unparsed []string
}

method:

1
2
// 返回cookie序列化的结果
func (c *Cookie) String() string

Request

代表一个服务端接收的或客户端发送的http请求.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
type Request struct {
    Method string
    URL *url.URL
    Proto string // "HTTP/1.0"
    ProtoMajor int // 1
    ProtoMinor int // 0
    Header Header
    Body io.ReadCloser
    ContentLength int64
    TransferEncoding []string
    Close bool
    Host string
    Form url.Values
    PostForm url.Values
    MultipartForm *multipart.Form
    Trailer Header
    RemoteAddr string
    RequestURI string
    TSL *tls.ConnectionState
}

function:

1
2
3
func NewRequest(method, urlStr string, body io.Reader) (*Request, error)

func ReadRequest(b *bufio.Reader) (req *Request, err error)

method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// 解析并返回该请求r的header设置的cookie
func (r *Request) Cookies() []*Cookie

// 返回请求r中命名为name的cookie,如果未找到返回nil, ErrNoCookie.
func (r *Request) Cookie(name string) (*cookie, error)

//解析r.URL中的查询字符串,并将解析结果更新到r.Form字段.
// post和put的body会同时更新到r.PostForm和r.Form.
func (r *Request) ParseForm() error

// 将请求的主体作为multipart/form-data解析.
func (r *Request) ParseMultipartForm(maxMemory int64) error

Response

代表一个http请求的回复r

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
type Response struct {
    Status string
    StatusCode int
    Proto string
    ProtoMajor int
    ProtoMinor int
    Header Header
    Body io.ReadCloser
    ContentLength int64
    TransferEncoding []string
    Close bool
    Trailer Header
    Request *Request
    TLS *tls.ConnectionState
}

functions:

1
2
3
4
5
6
func ReadResponse(r *bufio.Reader, req *Request) (*Response, error)

func Head(url string) (resp *Response, err error)
func Get(url string) (resp *Response, err error)
func Post(url string, bodyType string, boyd io.Reader) (resp *Response, err error)
func PostForm(url string, data url.Values) (resp *Response, err error)

methods:

1
2
3
4
func (r *Response) ProtoAtLeast(major, minor int) bool

// 获取相应中的Set-Cookie设置的cookie
func (r *Response) Cookies() []*Cookie

ResponseWriter

用于构造http回复.

1
2
3
4
5
type ResponseWriter interface {
    Header() Header
    WriteHeader(int)
    Write([]byte) (int, error)
}

net/mail


net/smtp


net/rpc

function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// 在DefaultServer注册并公布rcvr方法.
func Register(rcvr interface{}) error

// 接收连接,将每个连接交给DefaultServer服务.会阻塞.
func Accept(lis net.Listener)

// 在单个连接执行DefaultServer,会阻塞.
func ServceConn(conn io.ReadWriteCloser)

func HandleHTTP()

Call

代表一个执行中或执行完毕的会话.

1
2
3
4
5
6
7
type Call struct {
    ServiceMethod string
    Args interface{}
    Reply interface{}
    Error error
    Done chan *Call
}

Client

rpc客户端

1
type client struct {}

function:

1
2
3
4
5
func NewClient(conn io.ReadWriteCloser) *Client

func Dial(network, address string) (*Client, error)

func DialHTTP(network, address string) (*Client, error)

method:

1
2
3
4
5
6
// 调用指定的方法,等待返回,将结果写入reply.
func (client *Client) Call(serviceMethod string, args interface{}, reply interface{}) error

func (client *Client) Go(serviceMethod string, args interface{}, reply interface{}, done chan *Call) *Call

func (client *Client) Close() error

net/textproto


net/url

1
scheme://[userinfo@]host/path[?query][#fragment]

URL

1
2
3
4
5
6
7
8
9
type URL struct {
    Scheme string
    Opaque string
    User *Userinfo
    Host string // host or host:port
    Path string
    RawQuery string
    Fragment string
}

function:

1
2
func Parse(rawurl string) (url *URL, err error)
func ParseRequestURI(rawurl string) (url *URL, err error)

methods:

Userinfo

VAlues