Created
April 13, 2022 02:12
-
-
Save manhdaovan/b17f3e05b443b9b26966b0c101a9449e to your computer and use it in GitHub Desktop.
Simple approach to get Goroutine ID
This file contains hidden or 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
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