Go语言的标准库net/http包提供了完整的HTTP协议实现,既可以快速搭建HTTP服务端,也能方便地发送HTTP客户端请求,是Go语言web开发领域最基础也最常用的包之一。

net/http包的正确导入方式
net/http包属于Go语言标准库,不需要额外安装依赖,直接导入即可使用。导入时需要注意路径的准确性,正确的导入语句如下:
import "net/http"
如果有多个标准库需要导入,可以使用括号组合导入,示例如下:
import (
"fmt"
"net/http"
)
注意不要写成错误的导入路径,比如net/HTTP、net/http/v2等都是无效的,Go编译器会直接提示导入错误。
使用net/http包搭建HTTP服务端
基础服务端实现
net/http包提供了http.HandleFunc和http.ListenAndServe两个核心函数,可以快速实现一个简单的HTTP服务端,处理客户端的请求并返回响应。
下面是一个返回固定文本响应的服务端示例:
package main
import (
"fmt"
"net/http"
)
// 定义请求处理函数
func helloHandler(w http.ResponseWriter, r *http.Request) {
// 设置响应头的内容类型
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
// 写入响应内容
fmt.Fprintf(w, "Hello, this is a response from net/http server")
}
func main() {
// 注册路由和处理函数,当访问根路径时触发helloHandler
http.HandleFunc("/", helloHandler)
// 启动服务,监听8080端口
fmt.Println("Server start at :8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Printf("Server start failed: %vn", err)
}
}
自定义路由匹配规则
除了根路径,也可以注册其他路径的处理函数,路径匹配遵循前缀匹配规则,示例如下:
package main
import (
"fmt"
"net/http"
)
func main() {
// 处理/api/user路径的请求
http.HandleFunc("/api/user", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is user api response")
})
// 处理/api/order路径的请求
http.HandleFunc("/api/order", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is order api response")
})
http.ListenAndServe(":8080", nil)
}
使用net/http包发送HTTP客户端请求
net/http包同样提供了客户端功能,可以发送GET、POST等常见类型的HTTP请求,获取服务端响应。
发送GET请求
发送GET请求可以使用http.Get函数,示例如下:
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
// 发送GET请求,访问本地8080端口的服务
resp, err := http.Get("http://127.0.0.1:8080/")
if err != nil {
fmt.Printf("Request failed: %vn", err)
return
}
// 延迟关闭响应体,避免资源泄漏
defer resp.Body.Close()
// 读取响应内容
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Read response failed: %vn", err)
return
}
fmt.Printf("Response status: %sn", resp.Status)
fmt.Printf("Response body: %sn", body)
}
发送POST请求
发送POST请求可以使用http.Post函数,示例如下:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func main() {
// 发送POST请求,携带表单数据
resp, err := http.Post("http://127.0.0.1:8080/api/user", "application/x-www-form-urlencoded", strings.NewReader("name=test&age=18"))
if err != nil {
fmt.Printf("Request failed: %vn", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Read response failed: %vn", err)
return
}
fmt.Printf("Response status: %sn", resp.Status)
fmt.Printf("Response body: %sn", body)
}
使用net/http包的常见注意事项
- 服务端启动后,如果没有特殊处理,会一直阻塞运行,直到程序被终止或者监听端口出现异常。
- 客户端请求完成后,一定要关闭响应体
resp.Body,否则会造成网络连接资源泄漏。 - 处理请求时,如果需要获取请求参数,可以通过
r.URL.Query()获取GET参数,通过r.ParseForm()后获取POST表单参数。 - 生产环境中不建议直接使用
http.ListenAndServe的默认配置,最好自定义http.Server结构体,设置合理的读写超时时间,避免资源被长时间占用。
下面是一个自定义Server结构体的示例:
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
server := &http.Server{
Addr: ":8080",
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Server with custom config")
})
fmt.Println("Custom server start at :8080")
server.ListenAndServe()
}