On Wed, Sep 23, 2020 at 6:17 PM Alex Mills <a...@channelmeter.com> 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.
Usually, this kind of information is placed in the http request context, so all the function down the call chain can have access to the variables in the context. You have to pass that context to the call chain. > > > 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 golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/07fb2e73-14a2-4559-a7d6-2010acbc7c51n%40googlegroups.com. -- 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 golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMV2RqqcLRazZXs%3DQCMPkffAi41yKkA%2BL5_TzBmrWMi9LBjo4Q%40mail.gmail.com.