2016-05-06 19:12


  • IT行业问题
  • 计算机技术
  • it技术
  • 编程语言问答
  • 互联网问答

Came accross the below function here. I noticed the last parameter is identified with _. What is the intent of this pattern?

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Welcome!
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douzhi9921 douzhi9921 5年前

    It means "ignore that parameter", the reason that they still need the last parameter here is because they want to pass it as type Handle to the function GET, which has the signature:

    type Handle func(http.ResponseWriter, *http.Request, Params)

    If you simply pass something like func Index(w http.ResponseWriter, r *http.Request) it will not be treated as type Handle.

    点赞 评论 复制链接分享
  • dream_high1026 dream_high1026 5年前

    As others pointed out, it is a blank identifier. For instance, consider the following example:

    func main() {
         nums := []int{5, 3, 4}
         max := nums[0]
         for _, num := range nums {
             if num > max {
                max = num
        fmt.Println("max:", max)

    If you aren't going to use an index value, you can just ignore storing it by using _ instead of a variable name.=.

    点赞 评论 复制链接分享
  • dtpn60029 dtpn60029 5年前

    _ is the blank identifier. It's in the signature to show that the value doesn't get used, so the signature will still match the methods of the interface.

    点赞 评论 复制链接分享
  • douzhu5900 douzhu5900 4年前

    Using "_" in place of a parameter name fulfills the obligations of a higher-level "function as a parameter" without getting a warning about an unused parameter. In your case, I believe the compiler is told to ignore all incoming "POST" data, thus in effect reducing the request to the functionality of a "GET".

    点赞 评论 复制链接分享