Not sure about that. Context can store a map. I could map: request => userid
but then I would have to pass the request to the logging statements, so that would put me back at square one. There appears to be a way to get a reference on the goroutine id: http://blog.sgmansfield.com/2015/12/goroutine-ids/ so if I could have a global map of: goroutineid => userid then in the current goroutine, I could look in the global map to see if the id is in there? if the id is not in there, it's not really a fatal problem, so it can be fuzzy. so before it looks like: func Warn(r *http.Request, ...args){ var id = context.Get(r) fmt.Println("WARN: logged in user id", args...) } the new func would look like: var m = map[string]string{} func Warn(..args){ var gid = getGoroutineId() var loggedInUserId = m[gid] fmt.Println("WARN: logged in user id:", loggedInUserId, args...) } make sense? But I wonder if it's performant to using this get the goroutine id heavily: func getGID() uint64 { b := make([]byte, 64) b = b[:runtime.Stack(b, false)] b = bytes.TrimPrefix(b, []byte("goroutine ")) b = b[:bytes.IndexByte(b, ' ')] n, _ := strconv.ParseUint(string(b), 10, 64) return n } -alex On Wed, Sep 23, 2020 at 5:25 PM Joop Kiefte <iko...@gmail.com> wrote: > 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> [image: > p4aw3] > > On September 24, 2020 at 0:17 GMT, 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. > > > 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 > <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 golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAL6oN2fi-Nk24srp%3DcP_kdLOO6%2BLZJW8bo7-kaFHO%3DrfWuZsow%40mail.gmail.com.