Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ID ¶
func ID() int64
ID returns calling goroutine ID
Example ¶
package main
import (
"fmt"
"path"
"runtime"
"github.com/nikandfor/goid"
)
func main() {
c := make(chan struct{})
loc := func(pc uintptr) string {
f := runtime.FuncForPC(pc)
if f == nil {
return ""
}
file, line := f.FileLine(pc)
file = path.Base(file)
return fmt.Sprintf("%v:%v", file, line)
}
f := func(c chan struct{}) {
defer close(c)
id := goid.ID() // this one is simple
g := goid.GoPC() // location of "go ...()" instruction
s := goid.StartPC() // root (the most parent) function entry instruction of that goroutine
n := runtime.FuncForPC(s).Name()
n = path.Base(n)
fmt.Printf("goroutine 0x%x task %v (%v) created at %v", id, loc(s), n, loc(g))
}
go f(c)
<-c // wait for f to finish
// // Output:
// goroutine 0xc task goid_example_test.go:19 (goid.ExampleID.func2) created at goid_example_test.go:35
}
Output:
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.