This article will introduce you to the relevant knowledge about Golang and talk about how to quickly implement the Go Http Server framework. I hope it will be helpful to everyone.
If you want to use http server in Go, the easiest way is to use http/net
err := http.ListenAndServe(":8080", nil)if err != nil { panic(err.Error())}http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) { writer.Write([]byte("Hello"))})
define handle func [Related recommendations: Go video tutorial]
type HandlerFunc func(ResponseWriter, *Request)
The http server implementation of the standard library is very simple. Open a port and register a handler## that implements HandlerFunc
func main() { err := http.ListenAndServe(":8080", &Engine{}) if err != nil { panic(err.Error()) }}type Engine struct {}func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/hello" { w.Write([]byte("Hello")) }}
type Handler interface { ServeHTTP(ResponseWriter, *Request)}
import ( "net/http")type Engine struct { addr string route map[string]handFunc}type Context struct { w http.ResponseWriter r *http.Request handle handFunc}type handFunc func(ctx *Context) errorfunc NewServer(addr string) *Engine { return &Engine{ addr: addr, route: make(map[string]handFunc), }}func (e *Engine) Run() { err := http.ListenAndServe(e.addr, e) if err != nil { panic(err) }}func (e *Engine) Get(path string, handle handFunc) { e.route[path] = handle}func (e *Engine) handle(writer http.ResponseWriter, request *http.Request, handle handFunc) { ctx := &Context{ w: writer, r: request, handle: handle, } ctx.Next()}func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) { handleF := e.route[req.URL.Path] e.handle(w, req, handleF)}func (c *Context) Next() error { return c.handle(c)}func (c *Context) Write(s string) error { _, err := c.w.Write([]byte(s)) return err}
func TestHttp(t *testing.T) { app := NewServer(":8080") app.Get("/hello", func(ctx *Context) error { return ctx.Write("Hello") }) app.Run()}
package httpimport ( "net/http")type Engine struct { addr string route map[string]handFunc middlewares []handFunc}type Context struct { w http.ResponseWriter r *http.Request index int handlers []handFunc}type handFunc func(ctx *Context) errorfunc NewServer(addr string) *Engine { return &Engine{ addr: addr, route: make(map[string]handFunc), middlewares: make([]handFunc, 0), }}func (e *Engine) Run() { err := http.ListenAndServe(e.addr, e) if err != nil { panic(err) }}func (e *Engine) Use(middleware handFunc) { e.middlewares = append(e.middlewares, middleware)}func (e *Engine) Get(path string, handle handFunc) { e.route[path] = handle}func (e *Engine) handle(writer http.ResponseWriter, request *http.Request, handle handFunc) { handlers := make([]handFunc, 0, len(e.middlewares)+1) handlers = append(handlers, e.middlewares...) handlers = append(handlers, handle) ctx := &Context{ w: writer, r: request, index: -1, handlers: handlers, } ctx.Next()}func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) { handleF := e.route[req.URL.Path] e.handle(w, req, handleF)}func (c *Context) Next() error { c.index++ if c.index < len(c.handlers) { return c.handlers[c.index](c) } return nil}func (c *Context) Write(s string) error { _, err := c.w.Write([]byte(s)) return err}
func TestHttp(t *testing.T) { app := NewServer(":8080") app.Get("/hello", func(ctx *Context) error { fmt.Println("Hello") return ctx.Write("Hello") }) app.Use(func(ctx *Context) error { fmt.Println("A1") return ctx.Next() }) app.Use(func(ctx *Context) error { err := ctx.Next() fmt.Println("B1") return err }) app.Run()}
=== RUN TestHttp A1 Hello B1
The above is the detailed content of How to quickly implement the Go Http Server framework? Done in one article. For more information, please follow other related articles on the PHP Chinese website!