관리 메뉴

A seeker after truth

net/http 패키지에서 자주 쓰이는 것들 메모 (from 공식 reference) 본문

Programming Language/Golang(Go)

net/http 패키지에서 자주 쓰이는 것들 메모 (from 공식 reference)

dr.meteor 2019. 11. 27. 15:45

1. type Request(구조체)

A Request represents an HTTP request received by a server or to be sent by a client. The field semantics differ slightly between client and server usage. 

https://golang.org/pkg/net/http/#Request

 

2. type ResponseWriter(인터페이스)

A ResponseWriter interface is used by an HTTP handler to construct an HTTP response.

https://golang.org/pkg/net/http/#ResponseWriter

 

3. func HandleFunc

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

HandleFunc registers the handler function for the given pattern in the DefaultServeMux. The documentation for ServeMux explains how patterns are matched.

https://golang.org/pkg/net/http/#HandleFunc

 

4. type Server(구조체)

A Server defines parameters for running an HTTP server. The zero value for Server is a valid configuration.

https://golang.org/pkg/net/http/#Server

- 이 구조체 속 첫번째 필드: Addr string // TCP address to listen on, ":http" if empty

 

5. func ListenAndServe

func (srv *Server) ListenAndServe() error

ListenAndServe listens on the TCP network address srv.Addr and then calls Serve to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives. If srv.Addr is blank, ":http" is used. ListenAndServe always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.