Methods 参数传入Go
假设我有一个类型的方法 我想把它包装在另一个相同类型的方法上,有点像这样:Methods 参数传入Go,methods,parameter-passing,go,Methods,Parameter Passing,Go,假设我有一个类型的方法 我想把它包装在另一个相同类型的方法上,有点像这样: func MyWrapper(res http.ResponseWriter, req *http.Request) { // do stuff AnotherMethod(res, req) // <- question refers to this line // do more stuff } func AnotherMethod(res http.ResponseWrite
func MyWrapper(res http.ResponseWriter, req *http.Request) {
// do stuff
AnotherMethod(res, req) // <- question refers to this line
// do more stuff
}
func AnotherMethod(res http.ResponseWriter, req *http.Request) {
// main logic
}
func MyWrapper(res http.ResponseWriter,req*http.Request){
//做事
AnotherMethod(res,req)/是一种接口类型。这意味着它既可以是引用类型,也可以是值类型,具体取决于底层类型及其实现接口的方式
在本例中,res
是未报告类型*http.response
的一个实例。如您所见,它是指针类型,这意味着您可以在不创建整个结构副本的情况下传递它
要查看接收到的接口值中保存的实际类型,可以执行以下操作:fmt.Printf(“%T\n”,res)
。它应该打印:*http.resonse
有关接口类型在Go中如何工作的更多信息,我建议阅读有关该主题的。是一种接口类型。这意味着它既可以是引用类型,也可以是值类型,具体取决于基础类型以及它如何实现接口
在本例中,res
是未报告类型*http.response
的一个实例。如您所见,它是指针类型,这意味着您可以在不创建整个结构副本的情况下传递它
要查看接收到的接口值中保存的实际类型,可以执行以下操作:fmt.Printf(“%T\n”,res)
。它应该打印:*http.resonse
有关Go中界面类型如何工作的更多信息,我建议您阅读有关该主题的文章。先生,您太棒了。非常感谢。先生,您太棒了。非常感谢。
func MyWrapper(res http.ResponseWriter, req *http.Request) {
// do stuff
AnotherMethod(res, req) // <- question refers to this line
// do more stuff
}
func AnotherMethod(res http.ResponseWriter, req *http.Request) {
// main logic
}