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

Cam*_*spo 9 go

这里有以下功能.我注意到最后一个参数是用_.这种模式的目的是什么?

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Welcome!\n")
}
Run Code Online (Sandbox Code Playgroud)

小智 11

它意味着"忽略那个参数",这里他们仍然需要最后一个参数的原因是因为他们想把它传递type HandleGET具有签名的函数:

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

如果你只是传递类似的东西func Index(w http.ResponseWriter, r *http.Request)将不被视为type Handle.


mat*_*t.s 5

_是空白标识符.它在签名中显示该值未被使用,因此签名仍将与接口的方法匹配.