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")
}
相關文章
相關標籤/搜索