Golang

关注公众号 jb51net

关闭
首页 > 脚本专栏 > Golang > Go net/http构建RESTful API

Go语言使用net/http构建一个RESTful API的示例代码

作者:程序员爱钓鱼

Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如 Gin、Echo)已经封装了很多功能,但如果你真正想理解 Go的底层Web服务机制,那么使用net/http实现一个原生的RESTful API 是最好的入门方式,需要的朋友可以参考下本文

引言

Go 的标准库 net/http 提供了构建 Web 服务所需的强大功能。虽然众多第三方框架(如 Gin、Echo)已经封装了很多功能,但如果你真正想理解 Go 的底层 Web 服务机制,那么使用 net/http 实现一个原生的 RESTful API 是最好的入门方式。

本文将通过一个实战案例:构建一个用户信息管理 API,带你从零开始打造 RESTful 服务。

一、什么是 RESTful API?

REST(Representational State Transfer)是一种软件架构风格,常用于 Web API 的设计。

常见 HTTP 方法与操作语义:

方法语义说明
GET获取资源读取某个资源的内容
POST创建资源向服务器添加新资源
PUT更新资源修改已有资源
DELETE删除资源移除某个资源

二、实战目标:用户信息管理 API

我们将构建以下功能的 API:

三、代码实现

1. 用户数据结构

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "math/rand"
    "net/http"
    "strconv"
    "strings"
    "sync"
)

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

var (
    users   = make(map[int]User)
    mu      sync.Mutex
)

2. 工具函数:ID提取

func parseID(path string) (int, error) {
    parts := strings.Split(path, "/")
    if len(parts) < 3 {
        return 0, fmt.Errorf("invalid path")
    }
    return strconv.Atoi(parts[2])
}

3. 处理函数

func usersHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")

    switch r.Method {
    case "GET":
        // 获取所有用户
        mu.Lock()
        list := make([]User, 0, len(users))
        for _, user := range users {
            list = append(list, user)
        }
        mu.Unlock()
        json.NewEncoder(w).Encode(list)

    case "POST":
        // 创建用户
        var user User
        if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
            http.Error(w, err.Error(), http.StatusBadRequest)
            return
        }
        user.ID = rand.Intn(10000)
        mu.Lock()
        users[user.ID] = user
        mu.Unlock()
        json.NewEncoder(w).Encode(user)

    default:
        http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
    }
}
func userHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")

    id, err := parseID(r.URL.Path)
    if err != nil {
        http.Error(w, "Invalid ID", http.StatusBadRequest)
        return
    }

    mu.Lock()
    user, exists := users[id]
    mu.Unlock()

    switch r.Method {
    case "GET":
        if !exists {
            http.NotFound(w, r)
            return
        }
        json.NewEncoder(w).Encode(user)

    case "PUT":
        if !exists {
            http.NotFound(w, r)
            return
        }
        var updated User
        if err := json.NewDecoder(r.Body).Decode(&updated); err != nil {
            http.Error(w, err.Error(), http.StatusBadRequest)
            return
        }
        updated.ID = id
        mu.Lock()
        users[id] = updated
        mu.Unlock()
        json.NewEncoder(w).Encode(updated)

    case "DELETE":
        if !exists {
            http.NotFound(w, r)
            return
        }
        mu.Lock()
        delete(users, id)
        mu.Unlock()
        w.WriteHeader(http.StatusNoContent)

    default:
        http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
    }
}

4. 主函数启动服务

func main() {
    http.HandleFunc("/users", usersHandler)
    http.HandleFunc("/users/", userHandler)

    fmt.Println("服务启动中: http://localhost:8080")
    log.Fatal(http.ListenAndServe(":8080", nil))
}

四、测试方式(推荐使用 curl 或 Postman)

# 创建用户
curl -X POST -H "Content-Type: application/json" \
     -d '{"name":"Tom","age":25}' http://localhost:8080/users

# 获取所有用户
curl http://localhost:8080/users

# 获取指定用户
curl http://localhost:8080/users/1234

# 更新用户
curl -X PUT -H "Content-Type: application/json" \
     -d '{"name":"Jerry","age":30}' http://localhost:8080/users/1234

# 删除用户
curl -X DELETE http://localhost:8080/users/1234

五、总结与建议

通过本案例,我们学习了如何用 Go 原生的 net/http

虽然这个例子是最基础的,但它奠定了你进一步使用更强大框架(如 Gin、Echo)的基础。

六、进阶推荐

以上就是Go语言使用net/http构建一个RESTful API的示例代码的详细内容,更多关于Go net/http构建RESTful API的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文