如何用Golang写一个简单Web服务_Go语言HTTP项目实战

Go HTTP服务需显式处理路由、解析、超时等所有细节:用http.HandleFunc或ServeMux注册函数,手动调用ParseForm解析表单,通过http.Server设置Read/Write/Idle超时,无默认行为,全靠开发者明确控制。

用 Go 写一个简单 Web 服务,net/http 标准库完全够用,不需要引入任何第三方框架。

http.HandleFunc 注册路由最直接

Go 的 HTTP 路由本质是函数注册:URL 路径映射到处理函数。没有中间件、没有路由树、不自动解析参数——干净但需手动控制。

  • http.HandleFunc 只接受 func(http.ResponseWriter, *http.Request) 类型,不能传额外参数(如数据库连接),需闭包或结构体方法承接
  • 路径匹配是前缀式:注册 "/api" 会同时匹配 /api/api/users/api/xxx/yyy
  • 根路径 "/" 必须显式注册,不会自动 fallback
package main

import ( "fmt" "net/http" )

func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, World!") })

http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    fmt.Fprint(w, "ok")
})

http.ListenAndServe(":8080", nil)

}

http.ServeMux 显式管理路由更可控

直接传 nilhttp.ListenAndServe 会使用默认多路复用器(DefaultServeMux),全局共享且无法定制错误行为。生产中建议显式构造 *http.ServeMux

  • 可独立测试路由逻辑(传入 fake *http.Requesthttptest.ResponseRecorder
  • 避免和其他包(如某些监控库)意外注册冲突路径
  • 方便后续替换为自定义多路复用器(比如支持正则或 RESTful 路径)
package main

import ( "fmt" "net/http" )

func main() { mux := http.NewServeMux()

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Root handler")
})

mux.HandleFunc("/user/", func(w http.ResponseWriter, r *http.Request) {
    // 注意:r.URL.Path 是原始路径,/user/abc → Path = "/user/abc"
    id := r.URL.Path[len("/user/"):]
    fmt.Fprintf(w, "User ID: %s", id)
})

http.ListenAndServe(":8080", mux)

}

处理 POST 请求和表单数据要手动调用 r.ParseForm

Go 不会自动解析请求体。即使 Content-Type 是 application/x-www-form-urlencodedmultipart/form-data,也必须显式调用解析方法,否则 r.FormValue 返回空字符串。

  • r.ParseForm() 会读取并缓存整个请求体,多次调用无副作用,但首次调用会触发实际读取
  • 若需读取原始 JSON 或其他格式,应改用 io.ReadAll(r.Body),且不能再调用 ParseForm(Body 已关闭)
  • 文件上传需用 r.ParseMultipartForm,并注意设置 MaxMemory 防止内存溢出
package main

import ( "fmt" "net/http" )

func main() { http.HandleFunc("/login", func(w http.ResponseW

riter, r *http.Request) { if r.Method == "POST" { // 必须先解析,否则 FormValue 为空 if err := r.ParseForm(); err != nil { http.Error(w, "parse form error", http.StatusBadRequest) return } user := r.FormValue("username") pass := r.FormValue("password") fmt.Fprintf(w, "Login attempt: %s / %s", user, pass) return }

    // GET 返回登录页
    w.Header().Set("Content-Type", "text/html")
    fmt.Fprint(w, `
`) }) http.ListenAndServe(":8080", nil)

}

启动时监听地址和超时配置不能忽略

http.ListenAndServe 看似简单,但默认无读写超时、无空闲超时,线上长期运行易积累僵死连接,还可能被恶意长连接拖垮。

  • &http.Server{} 替代裸调用,显式控制超时时间
  • ReadTimeoutWriteTimeout 应设为合理值(如 5–30 秒),防止慢客户端阻塞 goroutine
  • IdleTimeout 控制 keep-alive 连接最大空闲时间,推荐设为 30–60 秒
  • 监听地址写成 ":8080" 表示所有接口,如需绑定特定 IP(如仅内网),应写全 "127.0.0.1:8080"
package main

import ( "fmt" "net/http" "time" )

func main() { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "OK") })

server := &http.Server{
    Addr:         ":8080",
    Handler:      mux,
    ReadTimeout:  5 * time.Second,
    WriteTimeout: 10 * time.Second,
    IdleTimeout:  30 * time.Second,
}

fmt.Println("Server starting on :8080")
server.ListenAndServe()

}

真正难的不是写几行 http.HandleFunc,而是理解 Go HTTP 模型里“无隐式行为”这个前提——所有解析、超时、重定向、状态码都要亲手指定,没默认魔法,也没隐藏陷阱。一旦习惯这种显式风格,反而比用框架更不容易漏掉关键配置。