gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:10:01 24 4
gpt4 key购买 nike

遇到下面的函数here .我注意到最后一个参数用 _ 标识。这种模式的意图是什么?

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

最佳答案

意思是“忽略那个参数”,这里还需要最后一个参数是因为他们想把它作为type Handle传递给函数GET,其中有签名:

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

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

关于go - Go 函数参数中下划线的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37079820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com