首先使用gin搭建一个服务,为我们的代码使用
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"time"
)
func main() {
r := gin.Default()
r.GET("/user", func(c *gin.Context) {
time.Sleep(10 * time.Second)
c.JSON(http.StatusOK, gin.H{
"message": "获取user成功",
"data": "100",
"err": "",
})
})
r.POST("/user", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "添加user成功",
"data": "100",
"err": "",
})
})
r.PUT("/user", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "修改user成功",
"data": "100",
"err": "",
})
})
r.DELETE("/user", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "删除user成功",
"data": "100",
"err": "",
})
})
r.Run() // listen and serve on 0.0.0.0:8080
}
然后开始我们的代码
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
//OptionFun 函数的参数
type OptionFun func(client *http.Client)
//with
func withTimeout(duration time.Duration) OptionFun {
return func(client *http.Client) {
client.Timeout = duration * time.Microsecond
}
}
func NewByOption(opts ...OptionFun) http.Client {
client := http.Client{}
for _, opt := range opts {
opt(&client)
}
return client
}
func main() {
GetTest()
PostTest()
PutTest()
DeleteTest()
TimeoutTest()
}
func GetTest() {
reqest,err := http.NewRequest(http.MethodGet,"http://127.0.0.1:8080/user",nil)
if err != nil {
fmt.Println("http.NewRequest :",err)
}
//创建http客户端
var c = NewByOption()
response,err := c.Do(reqest)
defer response.Body.Close()
if response.StatusCode != 200 {
fmt.Println("response.StatusCode :",response.StatusCode)
}
body, _ := io.ReadAll(response.Body) // 读取响应 body, 返回为 []byte
bodyMap := make(map[string]interface{})
err = json.Unmarshal(body,&bodyMap)
fmt.Println(bodyMap)
}
func PostTest() {
//POST
//在 POST 方式一般常用的为 2 中,
//
//通过 kv 形式传送,例如 form-data 和 x-www-form-urlencoded
//通过 json 形式传送,例如 application/json
//kv 形式
//payload := strings.NewReader("a=111")
//json 形式
payload := strings.NewReader("{\"name\":\"张三\"}")
reqest,err := http.NewRequest(http.MethodPost,"http://127.0.0.1:8080/user",payload)
if err != nil {
fmt.Println("http.NewRequest",err)
}
reqest.Header.Add("Content-Type", "application/json")
//创建http客户端
var c = NewByOption()
response,err := c.Do(reqest)
if err != nil {
fmt.Println("c.Do",err)
}
defer response.Body.Close()
if response.StatusCode != 200 {
fmt.Println("response.StatusCode :",response.StatusCode)
}
body, _ := io.ReadAll(response.Body) // 读取响应 body, 返回为 []byte
bodyMap := make(map[string]interface{})
err = json.Unmarshal(body,&bodyMap)
fmt.Println(bodyMap)
}
func PutTest() {
//由于 net/http 没有提供简化的 PUT 请求,这里需要使用 http.NewRequest 来创建请求
payload := strings.NewReader("{\"name\":\"张三\"}")
reqest,err := http.NewRequest(http.MethodPut,"http://127.0.0.1:8080/user",payload)
if err != nil {
fmt.Println("http.NewRequest",err)
}
reqest.Header.Add("Content-Type", "application/json")
//创建http客户端
var c = NewByOption()
response,err := c.Do(reqest)
if err != nil {
fmt.Println("c.Do",err)
}
defer response.Body.Close()
if response.StatusCode != 200 {
fmt.Println("response.StatusCode :",response.StatusCode)
}
body, _ := io.ReadAll(response.Body) // 读取响应 body, 返回为 []byte
bodyMap := make(map[string]interface{})
err = json.Unmarshal(body,&bodyMap)
fmt.Println(bodyMap)
}
func DeleteTest() {
payload := strings.NewReader("{\"name\":\"张三\"}")
reqest,err := http.NewRequest(http.MethodDelete,"http://127.0.0.1:8080/user",payload)
if err != nil {
fmt.Println("http.NewRequest",err)
}
reqest.Header.Add("Content-Type", "application/json")
//创建http客户端
var c = NewByOption()
response,err := c.Do(reqest)
if err != nil {
fmt.Println("c.Do",err)
}
defer response.Body.Close()
if response.StatusCode != 200 {
fmt.Println("response.StatusCode :",response.StatusCode)
}
body, _ := io.ReadAll(response.Body) // 读取响应 body, 返回为 []byte
bodyMap := make(map[string]interface{})
err = json.Unmarshal(body,&bodyMap)
fmt.Println(bodyMap)
}
func TimeoutTest() {
reqest,err := http.NewRequest(http.MethodGet,"http://127.0.0.1:8080/user",nil)
if err != nil {
fmt.Println("http.NewRequest :",err)
}
//创建http客户端
//var c = NewByOption() //不报错
//var c = NewByOption(withTimeout(3)) //不报错
var c = NewByOption(withTimeout(1)) //报错
response,err := c.Do(reqest)
if err != nil {
fmt.Println("c.Do(reqest) err ======",err)
return
}
//fmt.Println("response.Body==",response.Body)
defer response.Body.Close()
if response.StatusCode != 200 {
fmt.Println("response.StatusCode :",response.StatusCode)
}
body, _ := io.ReadAll(response.Body) // 读取响应 body, 返回为 []byte
bodyMap := make(map[string]interface{})
err = json.Unmarshal(body,&bodyMap)
fmt.Println(bodyMap)
}
运行
PS D:\go\src\options2> go run .\main.go
map[data:100 err: message:获取user成功]
map[data:100 err: message:添加user成功]
map[data:100 err: message:修改user成功]
map[data:100 err: message:删除user成功]
c.Do(reqest) err ====== Get "http://127.0.0.1:8080/user": context deadline exceeded (Client.Timeout exceeded while awaiting headers)