Querystring parameters

  1. func main() {
  2. router := gin.Default()
  3. // Query string parameters are parsed using the existing underlying request object.
  4. // The request responds to a url matching: /welcome?firstname=Jane&lastname=Doe
  5. router.GET("/welcome", func(c *gin.Context) {
  6. firstname := c.DefaultQuery("firstname", "Guest")
  7. lastname := c.Query("lastname") // shortcut for c.Request.URL.Query().Get("lastname")
  8. c.String(http.StatusOK, "Hello %s %s", firstname, lastname)
  9. })
  10. router.Run(":8080")
  11. }