本文主要是介绍Go语言使用net/http构建一个RESTful API的示例代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但...
引言
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:
GET /users
:获取用户列表GET /users/{id}
:获取指定用户POST /users
:创建新用户PUT /users/{id}
:更新用户DELETE /users/{id}
:删除用户
三、代码实现
1. 用户数据结构
package main import ( "encoding/json" "fmt" "log" "mathChina编程/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 rChina编程.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) www.chinasem.cn } }
4. 主函数启动服务
func main() { http.HandleFunc("/users", usersHandler) http.HandleFunc("/users/", userHandler) fmt.Println("服务启动中: http://localhost:8080") log.Fatal(http.ListenAndServe(":8080", nil)) }
四、测试方式(推荐使用 curl 或 Postman)
# 创建YZqPSbKqOv用户
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
:
- ✅ 实现 RESTful 风格的接口
- ✅ 处理不同的 HTTP 方法
- ✅ 编解码 JSON 数据
- ✅ 线程安全地管理数据结构
虽然这个例子是最基础的,但它奠定了你进一步使用更强大框架(如 Gin、Echo)的基础。
六、php进阶推荐
以上就是Go语言使用net/http构建一个RESTful API的示例代码的详细内容,更多关于Go net/http构建RESTful API的资料请关注China编程(www.chinasem.cn)其它相关文章!
这篇关于Go语言使用net/http构建一个RESTful API的示例代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!