--
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/07fb2e73-14a2-4559-a7d6-2010acbc7c51n%40googlegroups.com.
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 Fri, Sep 25, 2020 at 10:35 AM Alex Besogonov
<alex.be...@gmail.com> wrote:
>
> Inheritable goroutine-locals would actually work just fine in Go. Moreover, Go actually has them in the form of pprof labels.
What should happen if one goroutine changes an inherited
goroutine-local variable?
On Friday, September 25, 2020 at 4:43:45 PM UTC-7 Ian Lance Taylor wrote:On Fri, Sep 25, 2020 at 10:35 AM Alex Besogonov
<alex.be...@gmail.com> wrote:
>
> Inheritable goroutine-locals would actually work just fine in Go. Moreover, Go actually has them in the form of pprof labels.
What should happen if one goroutine changes an inherited
goroutine-local variable?A Go-style goroutine-local system can be thought as a context.Context that is transparently assigned to a goroutine and its children. Thus if you want to change the value, you'd do something like "c := magic.GetCurrentContext(); c = c.WithValue(...); magic.SetContext(c);".This would affect only the current goroutine going forward, but not its children.
--> On Wednesday, September 23, 2020 at 5:55:50 PM UTC-7 Ian Lance Taylor wrote:
>>
>> On Wed, Sep 23, 2020 at 5:46 PM Alex Mills <al...@channelmeter.com> wrote:
>> >
>> > There appears to be a way to get a reference on the goroutine id:
>> >
>> > http://blog.sgmansfield.com/2015/12/goroutine-ids/
>>
>> But as you can see by reading that blog article, that is almost a joke.
>>
>> Go considers these things to be better handled explicitly, which is
>> why people are telling you to use a context.Context value. And, yes,
>> you'll want to use a Context aware logging package.
>>
>> In Go it's trivial to create new goroutines, and as soon as you do
>> that any goroutine-local-variable scheme falls apart. So Go has
>> consistently chosen to not provide that capability, and similarly to
>> not provide goroutine IDs. It's an intentional choice by the
>> language. There have been a number of discussions about this in the
>> past on this mailing list.
>>
>> Ian
>
> --
> 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/f78f2d12-89d2-494d-983a-a462f0124d82n%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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/7bc62278-5cbc-47fe-abd2-36172e4e8520n%40googlegroups.com.
A Go-style goroutine-local system can be thought as a context.Context that is transparently assigned to a goroutine and its children. Thus if you want to change the value, you'd do something like "c := magic.GetCurrentContext(); c = c.WithValue(...); magic.SetContext(c);".This would affect only the current goroutine going forward, but not its children.What happens if you want to invoke an operation by passing its arguments to an existing goroutine that does work on the current goroutine's behalf (aka a worker pool)?I don't think this pattern is that uncommon, and it would break the inheritable-variables model.
On Fri, Sep 25, 2020 at 6:29 PM Alex Besogonov <alex.be...@gmail.com> wrote:
> If you mutate the value, then only the current goroutine is affected (not even its existing children). Basically, the goroutine-local context is copied on new goroutine launch.
> That's how pprof labels work right now, btw.
Labels are not variables. Variables have values that can be changed.
> A Go-style goroutine-local system can be thought as a context.Context that is transparently assigned to a goroutine and its children. Thus if you want to change the value, you'd do something like "c := magic.GetCurrentContext(); c = c.WithValue(...); magic.SetContext(c);".
> This would affect only the current goroutine going forward, but not its children.
In general, Go's attitude is that explicit is better than implicit.
If you rigorously stick to the pattern you describe, it's the same as
passing the context.Context around explicitly (except that passing the
Context around explicitly is more flexible).