As far as I know, that is exactly what the Context package and customs are 
meant for.

[Joop Kiefte - Chat @ 
Spike](https://spikenow.com/r/a/?ref=spike-organic-signature&_ts=p4aw3)   
[p4aw3]

On September 24, 2020 at 0:17 GMT, Alex Mills <[email protected]> wrote:

Since by default all http requests coming to a go http server are on their own 
goroutine, I am wondering if there is a way to have some sort of "global" 
variable that is local to a goroutine if that makes sense, something like this, 
where "gork" is the namespace for variables available anywhere within a 
goroutine:

func PrintLoggedInUser(){
log.Println(gork.loggedInUser)
}

go func(){
var loggedInUser = "abc"
PrintLoggedInUser()
}()

go func(){
var loggedInUser = "def"
PrintLoggedInUser()
}()

why? i am looking to log the current logged in user id, without having to 
manually pass that id to every log call.

log.Warn("foo") // will log: "foo", "logged in user id:", abc
log.Error("bar") // will log: "bar", "logged in user id:", abc

but for now, I have to pass the id manually:

log.Warn(id, "foo") // will log: "foo", "logged in user id:", abc
log.Error(id, "bar") // will log: "bar", "logged in user id:", abc

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
[https://groups.google.com/d/msgid/golang-nuts/07fb2e73-14a2-4559-a7d6-2010acbc7c51n%40googlegroups.com](https://groups.google.com/d/msgid/golang-nuts/07fb2e73-14a2-4559-a7d6-2010acbc7c51n%40googlegroups.com?utm_medium=email&utm_source=footer).

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c-64693-kfg2rxrc-g6gdsm%3D1r8vgkm%402.gethop.com.

Reply via email to