我正在学习 Go 中的指针。并设法写出类似的东西:
func hello(){
fmt.Println("Hello World")
}
func main(){
pfunc := hello //pfunc is a pointer to the function "hello"
pfunc() //calling pfunc prints "Hello World" similar to hello function
}
有没有办法声明函数指针而不像上面那样定义它?
我们可以像用 C 语言那样写一些东西吗?
e.g. void (*pfunc)(void);
如果您使用签名,它就有效。没有指针。
type HelloFunc func(string)
func SayHello(to string) {
fmt.Printf("Hello, %s!\n", to)
}
func main() {
var hf HelloFunc
hf = SayHello
hf("world")
}
或者,您可以直接使用函数签名,而无需声明新类型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)