Irir的 Context 有两个方法,返回的是我们已经在前面的章节中提到的 net/http 标准的 http.ResponseWriterhttp.Request

    • Context.Request()
    • Context.ResponseWriter()

    然而,除了 Iris Context 提供的独特的 Iris 特性和帮助,为了更易于开发,我们提供了一些现有 net/http 功能的包装器。

    这里是完整的方法列表,在你处理 URL 查询字符串时可能帮助你。

    1. // URLParam returns true if the url parameter exists, otherwise false.
    2. URLParamExists(name string) bool
    3. // URLParamDefault returns the get parameter from a request,
    4. // if not found then "def" is returned.
    5. URLParamDefault(name string, def string) string
    6. // URLParam returns the get parameter from a request, if any.
    7. URLParam(name string) string
    8. // URLParamTrim returns the url query parameter with
    9. // trailing white spaces removed from a request.
    10. URLParamTrim(name string) string
    11. // URLParamTrim returns the escaped url query parameter from a request.
    12. URLParamEscape(name string) string
    13. // URLParamInt returns the url query parameter as int value from a request,
    14. // returns -1 and an error if parse failed.
    15. URLParamInt(name string) (int, error)
    16. // URLParamIntDefault returns the url query parameter as int value from a request,
    17. // if not found or parse failed then "def" is returned.
    18. URLParamIntDefault(name string, def int) int
    19. // URLParamInt32Default returns the url query parameter as int32 value from a request,
    20. // if not found or parse failed then "def" is returned.
    21. URLParamInt32Default(name string, def int32) int32
    22. // URLParamInt64 returns the url query parameter as int64 value from a request,
    23. // returns -1 and an error if parse failed.
    24. URLParamInt64(name string) (int64, error)
    25. // URLParamInt64Default returns the url query parameter as int64 value from a request,
    26. // if not found or parse failed then "def" is returned.
    27. URLParamInt64Default(name string, def int64) int64
    28. // URLParamFloat64 returns the url query parameter as float64 value from a request,
    29. // returns -1 and an error if parse failed.
    30. URLParamFloat64(name string) (float64, error)
    31. // URLParamFloat64Default returns the url query parameter as float64 value from a request,
    32. // if not found or parse failed then "def" is returned.
    33. URLParamFloat64Default(name string, def float64) float64
    34. // URLParamBool returns the url query parameter as boolean value from a request,
    35. // returns an error if parse failed or not found.
    36. URLParamBool(name string) (bool, error)
    37. // URLParams returns a map of GET query parameters separated by comma if more than one
    38. // it returns an empty map if nothing found.
    39. URLParams() map[string]string

    查询字符串参数通过使用已有的底层的 request 对象解析。这个请求响应一个匹配 /welcome?firstname=Jane&lastname=Doe 的 URL。

    • ctx.URLParam("lastname") == ctx.Request().URL.Query().Get("lastname")

    示例代码:

    1. app.Get("/welcome", func(ctx iris.Context) {
    2. firstname := ctx.URLParamDefault("firstname", "Guest")
    3. lastname := ctx.URLParam("lastname")
    4. ctx.Writef("Hello %s %s", firstname, lastname)
    5. })