golang 获取当前协程的id
windows:
进入go root目录
在$GOROOT/src/runtime下直接添加文件proc_id.go
内容
package runtime
func GoID() int64{
return getg().goid
}
linux:
创建一个脚本#### patch.sh
#!/bin/sh
cat > $(go env GOROOT)/src/runtime/proc_id.go << END_FILE
package runtime
func GoID() int64{
return getg().goid
}
END_FILE
在代码中使用
package main
import "runtime"
import "fmt"
import "sync"
func main(){
fmt.Printf("current go id is: %v\r\n",runtime.GoID())
var wg sync.WatiGroup
wg.Add(10)
for i:=1;i<=10;i++ {
go func(index int){
fmt.Printf("Goroutine [%v], go id is: %v \r\n",index,runtime.GoID())
wg.Done()
}(i)
}
wg.Wait()
}
世界上最遥远的距离,是我在if里你在else里,似乎一直相伴又永远分离;