最简Web服务器需用http.ListenAndServe(":8080", nil),端口格式必须含冒号;ResponseWriter需先设Header再写Body;安全退出需用http.Server+context控制Shutdown。
Go 自带 net/http 包,不需要额外依赖就能跑起一个可访问的 HTTP 服务。核心就是调用 http.ListenAndServe,传入监听地址和处理器。
常见错误是端口被占用或没加 :8080 这类冒号前缀——ListenAndServe 第一个参数必须是 "host:port" 格式,空字符串 "" 会默认绑定 ":http"(即 ":80"),普通用户权限下会失败。
":8080"(推荐开发时用)"127.0.0.1:8080" 或 "[::1]:8080"
nil 表示使用默认的 http.DefaultServeMux
package mainimport ( "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) }
http.ResponseWriter 不是 io.Writer 的简单别名,它封装了状态码、Header 和响应体三部分。直接用 fmt.Fprint 是因为它的底层实现了 io.Writer 接口,但要注意:一旦开始写 body,就无法再修改 status code 或 header。
容易踩的坑:
fmt.Fprint 之后调用 w.WriteHeader(404) —— 无效,状态码已隐式设为 200log.Printf 打印请求信息时,别误写成 w.Write,否则内容会发给浏览器
w.Header().Set("Content-Type", "application/json")
原生 ListenAndServe 是阻塞调用,进程收到 SIGINT(Ctrl+C)后会直接 kill,连接可能中断。加上 http.Server 结构体可手动控制生命周期。
关键点:
defer srv.Shutdown() —— Shutdown 需要另一个 goroutine 触发context.WithTimeout 控制关机等待时间,否则可能永久 hang 住srv.Serve 和 srv.Shutdown 不能在同一个 goroutine 里串行调用package mainimport ( "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)localhost:8080 成功,但浏览器打不开?确认 URL 是 http://localhost:8080/,不是 https 或少写了 /
:8080(等价于 [::]:8080),但某些系统防火墙或 Docker 网络会拦截,可临时换用 "0.0.0.0:8080" 显式声明curl -v http://localhost:8080 或无痕窗口Go 的 HTTP 服务器极简,但“简”不等于“没细节”。真正卡住人的往往不是语法,而是对 ListenAndServe 阻塞模型、ResponseWriter 写入时机、信号处理顺序这些隐含契约的理解偏差。