首页 > 后端开发 > Golang > 正文

如何在Golang中使用net/http创建HTTP服务器_处理路由和响应

P粉602998670
发布: 2025-12-23 20:18:08
原创
218人浏览过
Go 中用 net/http 创建 HTTP 服务器无需第三方框架,核心是 http.ServeMux 和 Handler;支持默认多路复用器、自定义路由、方法判断、JSON 响应、参数解析及日志/恢复中间件。

如何在golang中使用net/http创建http服务器_处理路由和响应

在 Go 中用 net/http 创建 HTTP 服务器非常简洁,不需要第三方框架也能轻松处理路由和响应。核心在于理解 http.ServeMux(多路复用器)的作用,以及如何通过 http.HandleFunc 或自定义 http.Handler 来分发请求。

基础 HTTP 服务器:监听与响应

最简服务只需几行代码:

启动一个监听 8080 端口的服务器,对根路径 / 返回纯文本响应:

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)
}
登录后复制

这里 nil 表示使用默认的 http.DefaultServeMux,它负责把请求路径映射到对应的处理函数。

立即学习go语言免费学习笔记(深入)”;

手动管理路由:使用自定义 ServeMux

显式创建 http.ServeMux 更清晰,也便于测试和复用:

  • 避免污染全局默认多路复用器
  • 可为不同子服务配置独立路由树
  • 方便注入中间件或统一日志

示例:

func main() {
    mux := http.NewServeMux()
    
    mux.HandleFunc("/api/users", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        fmt.Fprint(w, `{"users":[]}`)
    })
    
    mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(http.StatusOK)
        fmt.Fprint(w, "OK")
    })

    http.ListenAndServe(":8080", mux)
}
登录后复制

支持不同 HTTP 方法:检查 r.Method

net/http 不直接按方法(GET/POST/PUT)自动路由,需在 handler 内判断:

Linfo.ai
Linfo.ai

Linfo AI 是一款AI驱动的 Chrome 扩展程序,可以将网页文章、行业报告、YouTube 视频和 PDF 文档转换为结构化摘要。

Linfo.ai 151
查看详情 Linfo.ai
mux.HandleFunc("/posts", func(w http.ResponseWriter, r *http.Request) {
    switch r.Method {
    case "GET":
        // 返回文章列表
        w.Header().Set("Content-Type", "application/json")
        fmt.Fprint(w, `[{"id":1,"title":"Go入门"}]`)
    case "POST":
        // 创建新文章
        w.WriteHeader(http.StatusCreated)
        fmt.Fprint(w, `{"id":2,"title":"新文章"}`)
    default:
        http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
    }
})
登录后复制

注意:确保返回正确状态码和响应头,尤其是 JSON 接口要设 Content-Type: application/json

返回结构化数据:JSON 响应

json.Marshal 序列化结构体,并设置响应头:

type User struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}

mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
    user := User{ID: 123, Name: "Alice"}
    
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(user) // 自动调用 Marshal,更安全
})
登录后复制

推荐用 json.NewEncoder(w).Encode() 替代 json.Marshal() + fmt.Fprint,它能流式写入、避免内存拷贝,且自动处理错误(如编码失败时写入空响应)。

处理 URL 参数和表单数据

查询参数(?key=value):r.URL.Query().Get("page")
路径参数(需手动解析,如 /user/123):strings.Split(r.URL.Path, "/") 或正则提取
表单数据(POST 表单或 x-www-form-urlencoded):r.ParseForm() 后读 r.FormValue("username")
JSON 请求体:io.ReadAll(r.Body) 读取原始字节,再 json.Unmarshal

示例(读取 JSON 请求体):

mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
    if r.Method != "POST" {
        http.Error(w, "POST required", http.StatusMethodNotAllowed)
        return
    }

    var req struct {
        Username string `json:"username"`
        Password string `json:"password"`
    }
    if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
        http.Error(w, "Invalid JSON", http.StatusBadRequest)
        return
    }

    // 验证逻辑...
    w.Header().Set("Content-Type", "application/json")
    fmt.Fprint(w, `{"success":true}`)
})
登录后复制

添加简单中间件:日志与恢复

通过包装 handler 实现通用逻辑:

func loggingMiddleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Printf("→ %s %s\n", r.Method, r.URL.Path)
        next.ServeHTTP(w, r)
    })
}

func recoverMiddleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        defer func() {
            if err := recover(); err != nil {
                http.Error(w, "Internal error", http.StatusInternalServerError)
                fmt.Printf("Panic: %v\n", err)
            }
        }()
        next.ServeHTTP(w, r)
    })
}

// 使用:
mux := http.NewServeMux()
mux.HandleFunc("/api/", apiHandler)
http.ListenAndServe(":8080", recoverMiddleware(loggingMiddleware(mux)))
登录后复制

注意中间件顺序:越靠外的越先执行(如日志在 recover 外层,就能记录 panic 前的请求)。

以上就是如何在Golang中使用net/http创建HTTP服务器_处理路由和响应的详细内容,更多请关注php中文网其它相关文章!

路由优化大师
路由优化大师

路由优化大师是一款及简单的路由器设置管理软件,其主要功能是一键设置优化路由、屏广告、防蹭网、路由器全面检测及高级设置等,有需要的小伙伴快来保存下载体验吧!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号