is there a goroutine scope global veriable ?

170 views
Skip to first unread message

hui zhang

unread,
Jun 18, 2019, 10:52:54ā€ÆPM6/18/19
to golang-nuts
I write logs into one single log file.
and need to set a different tracking id for each goroutine.
But I don't want to pass the tracking id argument for all functions.


func main() {
Ā  go
Func()
}
func
Func() {
Ā 
SetLogTrackId("xxxx")
Ā 
LogDebug("123")
Ā  call1
()
}
func call1
() {
Ā  Ā 
LogDebug("call1")
}
var trackId string //need this scope to be goroutine scope
func
SetLogTrackId(id string) {
Ā  Ā  trackId
= id
}
func
LogDebug(v ...interface{}) {
Ā  Ā  log
.Println("DEBUG " + trackId + " " + fmt.Sprintln(v...))
}


Message has been deleted

hui zhang

unread,
Jun 18, 2019, 10:59:09ā€ÆPM6/18/19
to golang-nuts
is there a goroutine scope global veriable ?
likeĀ  fork a variable from main ?

Kurtis Rader

unread,
Jun 18, 2019, 11:03:59ā€ÆPM6/18/19
to hui zhang, golang-nuts
On Tue, Jun 18, 2019 at 7:56 PM hui zhang <fastf...@gmail.com> wrote:

is there a goroutine scope global veriable ?Ā Ā  soĀ  I can do this ?

You're asking if Go supports the equivalent of thread local storage as used in Java, C++, and Python. The answer is, no. SeeĀ https://stackoverflow.com/questions/31932945/does-go-have-something-like-threadlocal-from-javaĀ andĀ https://github.com/golang/go/issues/21355

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Jan Mercl

unread,
Jun 19, 2019, 4:16:48ā€ÆAM6/19/19
to hui zhang, golang-nuts
On Wed, Jun 19, 2019 at 4:55 AM hui zhang <fastf...@gmail.com> wrote:

> is there a goroutine scope global veriable ? so I can do this ?

Go has no global scope. The closest is universe scope but that scope
contains only predeclared identifiers. I think you are meaning package
scope.

Goroutines can access variables visible to them in any scope, but the
only ones that are private to the goroutine have block scope.

hui zhang

unread,
Jun 19, 2019, 4:41:19ā€ÆAM6/19/19
to golang-nuts
thanks ,Ā  I also google blew links.
It is called goroutine local storage ļ¼ˆglsļ¼‰Ā  andĀ  many third party implement(https://github.com/jtolds/gls)
however,Ā Ā  the golang official suggest to use go context.
but, very few document mention , how to do it as goroutine local storage .Ā  Can anyone give an example to solve my problem , refer the code I attached?


åœØ 2019幓6꜈19ę—„ę˜ŸęœŸäø‰ UTC+8äøŠåˆ11:03:59ļ¼ŒKurtis Rader写道ļ¼š

Jan Mercl

unread,
Jun 19, 2019, 4:55:09ā€ÆAM6/19/19
to hui zhang, golang-nuts
On Wed, Jun 19, 2019 at 10:41 AM hui zhang <fastf...@gmail.com> wrote:

> but, very few document mention , how to do it as goroutine local storage . Can anyone give an example to solve my problem , refer the code I attached?

Make the goroutine a method of a struct that contains any context the
goroutine needs. The data in the struct becomes your goroutine-local
storage.

type gls struct { id int }

func newG(id int) {
g := &gls{id}
go g.run()
}

func (g *gls) run() { fmt.Println(g.id) }

hui zhang

unread,
Jun 19, 2019, 5:15:38ā€ÆAM6/19/19
to golang-nuts
thank you very much.Ā 
Is there aĀ  golang context way to do it?

åœØ 2019幓6꜈19ę—„ę˜ŸęœŸäø‰ UTC+8äø‹åˆ4:55:09ļ¼ŒJan Mercl写道ļ¼š

Jan Mercl

unread,
Jun 19, 2019, 5:26:02ā€ÆAM6/19/19
to hui zhang, golang-nuts
On Wed, Jun 19, 2019 at 11:15 AM hui zhang <fastf...@gmail.com> wrote:

> Is there a golang context way to do it?

Some bits are at https://golang.org/pkg/context/#pkg-examples

hui zhang

unread,
Jun 19, 2019, 5:29:32ā€ÆAM6/19/19
to golang-nuts
this implement does not solve the problem.
still need pass the parameter in all functions.
func (g *gls) run()Ā Ā Ā  orĀ  Ā  func run (g *gls)

åœØ 2019幓6꜈19ę—„ę˜ŸęœŸäø‰ UTC+8äø‹åˆ4:55:09ļ¼ŒJan Mercl写道ļ¼š
On Wed, Jun 19, 2019 at 10:41 AM hui zhang <fastf...@gmail.com> wrote:

jake...@gmail.com

unread,
Jun 19, 2019, 12:09:10ā€ÆPM6/19/19
to golang-nuts
This has been discussed many times before. Searching "local storage" on this group brings up many discussions, including:


My take is that GLS could be genuinely useful in a few cases, but would undoubtedly lead to abuse in most cases. In the vast majority ofĀ  situations, what you really want to track, or log, is the specific work being done, not actually which goroutine it happens to be running on. For example, tracking a specific request, etc. My advice is to pass around a context, or some other identifying information about the specific work being done. At first this will seem tedious and annoying, but you will likely get used to it.

Robert Engels

unread,
Jun 19, 2019, 12:37:48ā€ÆPM6/19/19
to jake...@gmail.com, golang-nuts
Side-managed thread/execution context has been around since the concept of a thread. It is highly useful, and is more robust and secure than a Context object passed among methods.

If Go had any concept of a "secure runtime" it would be essential, but it doesn't, so you make do with what you have.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/53512dd1-8c24-460d-9bf5-646ea1e95d0f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Michael Jones

unread,
Jun 19, 2019, 2:50:11ā€ÆPM6/19/19
to Robert Engels, golang-nuts, jake...@gmail.com
For the OP:

A thought to share on the general topic: Go is pioneering a sufficiently different model of concurrent computation that it may not make much sense to ask for or seek equivalencies with classic thread/process models.Ā 

This says nothing against the standard model, nor against the ā€œmissingā€ services; it is a note to encourage design in Go along with coding in Go, instead of design in C/C++/Java/POSIX/... and a best-effort realization in Go.Ā 

By way of analogy, Iā€™ve long been involved in boat design and construction. We always use a pair Furuno or Simrad RADAR antennas, one of long range and one short, to optimize object detection/clutter rejection and as a redundancy safety factor. However, when sailing on a HobeeCat, iā€™ve never looked for nor missed the Radar or any other instruments of navigation. A HobieCat is just such a different idea of sailing that *all* the normal mechanisms have no place.Ā  There is no ā€œwhere do I stow the anchorā€ or ā€œwhere do I control the RAM signal lights.ā€ There are whole chapters of the USCG instructions (ColRegs) where the thing discussed is not on a HobeeCat. That does not make such boats inadequate; it makes them fun.Ā 

If you can stop thinking of concurrency in Go as a small group of exquisitely instrumented machines and instead imagine a swarm of ants, youā€™ll find new and interesting ways to solve problems. They often work even better, and are also more fun.Ā 


For more options, visit https://groups.google.com/d/optout.
--
Michael T. Jones
michae...@gmail.com

Henrik Johansson

unread,
Jun 20, 2019, 3:47:09ā€ÆAM6/20/19
to Michael Jones, Robert Engels, golang-nuts, jake...@gmail.com
On a practical note I think thread local storage is more or less discouraged in for example Java as well
because it makes all the new shine "reactive" tools break since they make no guarantees as to which thread you are running on.

That may or may not be relevant to your case but to say that it's standard practice is probably not true any more.

Robert Engels

unread,
Jun 20, 2019, 11:11:21ā€ÆAM6/20/19
to golang-nuts

Yes, Windows 3.1 is new again... A primary benefit for Go - lightweight parallelism and concurrency of procedural functions - is thrown out the "window" with reactive style programming. No need to use reactive if using Go properly.
Reply all
Reply to author
Forward
0 new messages