Golang编写Hello World Web服务器完整流程

最简Web服务器需用http.ListenAndServe(":8080", nil),端口格式必须含冒号;ResponseWriter需先设Header再写Body;安全退出需用http.Server+context控制Shutdown。

用 net/http 启动最简 Web 服务器

Go 自带 net/http 包,不需要额外依赖就能跑起一个可访问的 HTTP 服务。核心就是调用 http.ListenAndServe,传入监听地址和处理器。

常见错误是端口被占用或没加 :8080 这类冒号前缀——ListenAndServe 第一个参数必须是 "host:port" 格式,空字符串 "" 会默认绑定 ":http"(即 ":80"),普通用户权限下会失败。

  • 监听本地所有 IPv4/IPv6 接口:用 ":8080"(推荐开发时用)
  • 只监听 localhost:用 "127.0.0.1:8080""[::1]:8080"
  • 第二个参数传 nil 表示使用默认的 http.DefaultServeMux
package main

import ( "fmt" "net/http" )

func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello World") }) fmt.Println("Server starting on :8080") http.ListenAndServe(":8080", nil) }

为什么 HandleFunc 里要写 fmt.Fprint(w, ...)

http.ResponseWriter 不是 io.Writer 的简单别名,它封装了状态码、Header 和响应体三部分。直接用 fmt.Fprint 是因为它的底层实现了 io.Writer 接口,但要注意:一旦开始写 body,就无法再修改 status code 或 header。

容易踩的坑:

  • fmt.Fprint 之后调用 w.WriteHeader(404) —— 无效,状态码已隐式设为 200
  • log.Printf 打印请求信息时,别误写成 w.Write,否则内容会发给浏览器
  • 如果想返回 JSON,记得先设 header:w.Header().Set("Content-Type", "application/json")

如何让服务器支持 Ctrl+C 安全退出

原生 ListenAndServe 是阻塞调用,进程收到 SIGINT(Ctrl+C)后会直接 kill,连接可能中断。加上 http.Server 结构体可手动控制生命周期。

关键点:

  • 不能只靠 defer srv.Shutdown() —— Shutdown 需要另一个 goroutine 触发
  • 必须用 context.WithTimeout 控制关机等待时间,否则可能永久 hang 住
  • srv.Servesrv.Shutdown 不能在同一个 goroutine 里串行调用
package main

import ( "context" "fmt" "net/http" "os" "os/signal" "syscall" "time" )

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

srv := &http.Server{
    Addr:    ":8080",
    Handler: mux,
}

done := make(chan error, 1)
go func() {
    fmt.Println("Server starting on :8080")
    done <- srv.ListenAndServe()
}()

quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Println("Shutting down server...")

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := srv.Shutdown(ctx); err != nil {
    fmt.Printf("Server shutdown error: %v\n", err)
}
if err := <-done; err != nil && err != http.ErrServerClosed {
    fmt.Printf("Server ListenAndServe error: %v\n", err)
}

}

调试时常见的 404 或连接拒绝问题

启动后浏览器打不开,大概率不是代码问题,而是环境或理解偏差:

  • 运行程序后没看到 Server starting... 输出?检查是否 panic(比如端口被占,错误是 listen tcp :8080: bind: address already in use
  • curl localhost:8080 成功,但浏览器打不开?确认 URL 是 http://localhost:8080/,不是 https 或少写了 /
  • 从其他机器访问不到?默认绑定的是 :8080(等价于 [::]:8080),但某些系统防火墙或 Docker 网络会拦截,可临时换用 "0.0.0.0:8080" 显式声明
  • 修改代码后刷新页面还是旧内容?浏览器缓存导致,试试 curl -v http://localhost:8080 或无痕窗口

Go 的 HTTP 服务器极简,但“简”不等于“没细节”。真正卡住人的往往不是语法,而是对 ListenAndServe 阻塞模型、ResponseWriter 写入时机、信号处理顺序这些隐含契约的理解偏差。