我正在学习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
}
Run Code Online (Sandbox Code Playgroud)
有没有办法声明函数指针而不像上面那样定义它?我们可以写一些像C一样的东西吗?
例如 void (*pfunc)(void);
通道将通信 - 值的交换 - 与同步相结合 - 保证两个计算(goroutine)处于已知状态.
如何使用Google Go中的频道执行互斥锁功能?
package main
import "sync"
var global int = 0
var m sync.Mutex
func thread1(){
m.Lock()
global = 1
m.Unlock()
}
func thread2(){
m.Lock()
global = 2
m.Unlock()
}
func main(){
go thread1()
go thread2()
}
Run Code Online (Sandbox Code Playgroud)