web

[TOC]

0. 基于net/http搭建Http服务

package main

import (
    "net/http"
)

func main() {
    // 注册路由/hello并绑定处理函数
    http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
        writer.WriteHeader(http.StatusOK)
        writer.Write([]byte("hello world"))
    })
    // 启动HTTP服务并监听在端口8888,未指定Handler,使用默认mux
    //DefaultServeMux is the default ServeMux used by Serve.
    //var DefaultServeMux = &defaultServeMux
    http.ListenAndServe(":8888", nil)
}

/*
1. 请求
    curl http://127.0.0.1:8888/hello
1. 返回
    hello world

2. 请求
   curl http://127.0.0.1:8888/hell 
2. 返回
   404 page not found
*/

1. 自定义Handler Mux

2. 静态路由

3. 上下文 快速构造HTTP响应

4. 动态路由

5. 分组控制

6. 中间件

最后更新于

这有帮助吗?