mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 20:52:22 +00:00
35 lines
499 B
Go
35 lines
499 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
println("a:", GetGoid())
|
|
go func() {
|
|
println("b:", GetGoid())
|
|
}()
|
|
|
|
time.Sleep(time.Second)
|
|
}
|
|
|
|
func GetGoid() int64 {
|
|
var (
|
|
buf [64]byte
|
|
n = runtime.Stack(buf[:], false)
|
|
stk = strings.TrimPrefix(string(buf[:n]), "goroutine ")
|
|
)
|
|
|
|
idField := strings.Fields(stk)[0]
|
|
id, err := strconv.Atoi(idField)
|
|
if err != nil {
|
|
panic(fmt.Errorf("can not get goroutine id: %v", err))
|
|
}
|
|
|
|
return int64(id)
|
|
}
|