I am a beginner in Golang, and I would like to implement something similar to the Mapped Diagnostic Context (MDC) in Java, which is achieved through thread-local storage, in Go. However, I find it hard to find information about global thread-local storage in Go online.
I have several questions:
Is it possible to create a kind of global thread-local storage for each goroutine in Go to store data and context?
Is attempting to implement global thread-local storage considered an anti-pattern in Go?
Is it recommended to replace the way of passing context by implementing global thread-local storage in Go?
Assuming in your choice, would you prefer to use the method of passing context or would you attempt to implement a thread-local storage to save and manage context?
I've found some references to this, but I can't come to a conclusion to decide whether to implement it or not.
There are several issues here, which usually happen when you try to emulate features of other languages in Go.
func f() {
var i int
go func() {
// Here, use i as thread-local storage
}()
}
func f() {
go func() {
var t threadLocalData
...
g(t)
...
}{}
}