简介
Go 的 net/http 包是构建 HTTP 服务的核心库,功能强大且易于使用。它提供了基本的 HTTP 客户端和服务端支持,可以快速构建 REST API、Web 应用等服务。
本文将介绍 net/http 包的核心功能,并通过实际代码演示如何使用该包构建 HTTP 服务器和客户端。
目录
1. net/http 包核心概念
2. 构建一个 HTTP 服务器
3. 构建一个 HTTP 客户端
4. 演示代码:构建简单的 Web 应用
1. net/http 包核心概念
核心组件
- http.Handler 接口:
定义了处理 HTTP 请求的接口,只有一个方法:
type Handler interface {
ServeHTTP(w ResponseWriter, r *Request)
}
任意实现 ServeHTTP 的对象都可以用作 HTTP 处理器。
- http.ResponseWriter:
用于构建 HTTP 响应,提供写入功能,支持设置状态码和头部信息。
- http.Request:
包含客户端请求的所有信息,如 URL、方法、头部、正文等。
- http.ServeMux:
HTTP 请求多路复用器,用于将不同的 URL 路由到不同的处理器。
2. 构建一个 HTTP 服务器
下面是一个基本 HTTP 服务器的示例:
package main
import (
"fmt"
"net/http"
)
// 简单的处理器
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, World!")
}
func main() {
// 注册路由和处理器
http.HandleFunc("/", helloHandler)
// 启动服务器
fmt.Println("Starting server at :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Println("Error starting server:", err)
}
}
运行方法
- 保存代码为 main.go。
- 在终端运行:
go run main.go
- 在浏览器访问 http://localhost:8080/,会看到返回的 Hello, World!。
3. 构建一个 HTTP 客户端
net/http 包内置了简单的 HTTP 客户端,可以用来发送 HTTP 请求。
以下是一个 GET 请求的示例:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
// 发送 GET 请求
resp, err := http.Get("https://jsonplaceholder.typicode.com/posts/1")
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
// 读取响应正文
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return
}
fmt.Println("Response Status:", resp.Status)
fmt.Println("Response Body:", string(body))
}
运行方法
- 保存代码为 client.go。
- 在终端运行:
go run client.go
- 输出将包含从指定 URL 返回的响应状态和正文。
4. 演示代码:构建简单的 Web 应用
场景描述
构建一个简单的 Web 应用,包括以下功能:
- 主页:显示欢迎消息。
- 查询用户:通过 /user?id=123 查询用户信息。
- 创建用户:通过 POST 方法提交用户数据。
代码实现
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
)
// 模拟数据库
var users = map[int]string{
1: "Alice",
2: "Bob",
3: "Charlie",
}
// 主页处理器
func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Welcome to the User Management System!")
}
// 查询用户处理器
func getUserHandler(w http.ResponseWriter, r *http.Request) {
// 解析查询参数
idStr := r.URL.Query().Get("id")
id, err := strconv.Atoi(idStr)
if err != nil || id <= 0 {
http.Error(w, "Invalid user ID", http.StatusBadRequest)
return
}
// 查找用户
name, exists := users[id]
if !exists {
http.Error(w, "User not found", http.StatusNotFound)
return
}
// 返回用户信息
fmt.Fprintf(w, "User ID: %d, Name: %s\n", id, name)
}
// 创建用户处理器
func createUserHandler(w http.ResponseWriter, r *http.Request) {
// 检查请求方法
if r.Method != http.MethodPost {
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
return
}
// 读取请求正文
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
return
}
defer r.Body.Close()
// 解析用户数据
var user struct {
ID int `json:"id"`
Name string `json:"name"`
}
if err := json.Unmarshal(body, &user); err != nil {
http.Error(w, "Invalid JSON data", http.StatusBadRequest)
return
}
// 添加到用户列表
users[user.ID] = user.Name
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, "User created: ID=%d, Name=%s\n", user.ID, user.Name)
}
func main() {
// 路由注册
http.HandleFunc("/", homeHandler)
http.HandleFunc("/user", getUserHandler)
http.HandleFunc("/create", createUserHandler)
// 启动服务器
fmt.Println("Server started at :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Println("Error:", err)
}
}
运行方法
1. 保存代码为 web_app.go。
2. 在终端运行:
go run web_app.go
3. 测试 API:
? 访问主页:http://localhost:8080/
? 查询用户:http://localhost:8080/user?id=1
? 创建用户:
curl -X POST -d '{"id":4,"name":"Diana"}' -H "Content-Type: application/json" http://localhost:8080/create
总结
通过本文,你学习了如何使用 Golang 的 net/http 包构建 HTTP 服务器和客户端。这个包的强大之处在于其简单性和灵活性,适合快速开发各种 Web 应用和 API 服务。通过扩展可以进一步实现中间件、路由库集成(如 Gorilla Mux)等功能,满足更复杂的需求。