GET请求(postman):
![](/i/l/?n=22&i=blog/782793/202209/782793-20220925104902494-1736119699.png)
返回:
其实:count的值,每次调用一次,增加1(线程安全的)。
问题:这里的线程安全,你知道是什么意思吗?
===============
Post请求:
返回:
附:GO语言代码:
1 package main 2 import ( 3 "flag" 4 "fmt" 5 "github.com/gin-gonic/gin" 6 "net/http" 7 "sync" 8 "sync/atomic" 9 ) 10 11 var count int32 12 var count2 int32 13 var wg sync.WaitGroup 14 15 //go build FastGet.go 16 //get(postman): 17 //http://127.0.0.1:6677/user/query?name=kk&age=18 18 //返回:{name:'kk',age:18,count:7} 19 20 //post(postman): 21 //url: http://127.0.0.1:6677/user/post 22 //method: post 23 //body:urlencoded: name=mike,age=20 24 func main() { 25 fmt.Print("请求详情参见:https://www.cnblogs.com/xiaoyongdata/p/16727412.html") 26 var port string 27 flag.StringVar(&port , "port", "6677", "the author's name") 28 29 router := gin.Default() 30 router.GET("/user/query/", func(context *gin.Context) { 31 //获取name值 32 age := context.DefaultQuery("age","18") 33 name := context.DefaultQuery("name","mike") 34 //返回值 35 atomic.AddInt32(&count, 1) 36 context.String(http.StatusOK, "{name:'%s',age:%s,count:%d}", name,age,count) 37 }) 38 39 router.POST("/user/post/", func(context *gin.Context) { 40 //获取传递的值 41 name := context.PostForm("name") 42 age := context.DefaultPostForm("age","10") 43 atomic.AddInt32(&count2, 1) 44 //以JSON格式返回 45 context.JSON(http.StatusOK, gin.H{"status":gin.H{"status_code":http.StatusOK,"count":count2},"name":name,"age":age}) 46 }) 47 router.Run(":"+port) 48 }
标签:count,HttpServer,http,name,age,调用,context,go,gin From: https://www.cnblogs.com/xiaoyongdata/p/16727412.html