1. 代码:

  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. func main() {
  7. router := gin.Default()
  8. // Query string parameters are parsed using the existing underlying request object.
  9. // The request responds to a url matching: /welcome?firstname=Jane&lastname=Doe
  10. // 网址输入: localhost:8080/welcome?firstname=Jane&lastname=Doe
  11. router.GET("/welcome", func(c *gin.Context) {
  12. firstname := c.DefaultQuery("firstname", "Guest")
  13. lastname := c.Query("lastname") // shortcut for c.Request.URL.Query().Get("lastname")
  14. c.String(http.StatusOK, "Hello %s %s", firstname, lastname)
  15. })
  16. router.Run(":8080")
  17. }

2. 在网址输入

http://localhost:8080/welcome?firstname=Jane&lastname=%E6%99%BA%E5%8B%87

image.png