GIN-接口范例3-请求中的参数

func main() {
	router := gin.Default()

	// 请求参数被当前内部请求对象解析.
	// 当用户访问/welcome?firstname=Jane&lastname=Doe时候,将映射到这个方法
	router.GET("/welcome", func(c *gin.Context) {
		firstname := c.DefaultQuery("firstname", "Guest") //当firstname默认值为Guest
		lastname := c.Query("lastname") //  c.Request.URL.Query().Get("lastname") 的缩写

		c.String(http.StatusOK, "Hello %s %s", firstname, lastname)
	})
	router.Run(":8080")
}
相关文章
相关标签/搜索