Last active
August 29, 2015 14:13
-
-
Save mix3/2adb2e8c1970cf40d102 to your computer and use it in GitHub Desktop.
runtime.StackからgoroutineIDを取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"runtime" | |
"sync" | |
) | |
func goroutineId() string { | |
buf := make([]byte, 1024) | |
runtime.Stack(buf, false) | |
id := []byte{} | |
if string(buf[0:9]) == "goroutine" { | |
for _, c := range buf[10:] { | |
if string(c) == " " { | |
break | |
} | |
id = append(id, c) | |
} | |
} | |
return string(id) | |
} | |
func main() { | |
var wg sync.WaitGroup | |
for i := 0; i < 10; i++ { | |
wg.Add(1) | |
go func() { | |
log.Println(goroutineId()) | |
wg.Done() | |
}() | |
} | |
wg.Wait() | |
} |
Author
mix3
commented
Jan 11, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment