Skip to content

Instantly share code, notes, and snippets.

@manhdaovan
Created April 13, 2022 02:12
Show Gist options
  • Save manhdaovan/b17f3e05b443b9b26966b0c101a9449e to your computer and use it in GitHub Desktop.
Save manhdaovan/b17f3e05b443b9b26966b0c101a9449e to your computer and use it in GitHub Desktop.
Simple approach to get Goroutine ID
var goroutineID uint64
// Go is a simple replacement for `go` keyword, but returns its ID.
// Go also passes the goroutine ID to function `f`.
// Only use it when you want to use the ID attached with function `f` executed by a goroutine.
// Otherwise, just use nornal `go` keyword.
func Go(f func(goroutineID uint64)) uint64 {
id := atomic.AddUint64(&goroutineID, 1)
go f(id)
return id
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment