dongmaqiu6084 2016-05-06 19:12
浏览 150
已采纳

Go函数参数中下划线的含义

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!
")
}
  • 写回答

4条回答 默认 最新

  • douzhi9921 2016-05-06 19:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真