News for hackers. Stuff that matters.
Re:Down the Golang Nil Rabbit Hole by shp0ngle on Tuesday March 30, 2021 @01:31AM (#26631495)
Not him.

For me it's:

1. context.Context and putting everything under the sun there, untyped (which is a design fault, not just "people using it wrong")

2. reusing the looping variable in for loop

2.5. general awkwardness of go templating (I plan to write whole article about this, but too lazy to actually write it)

Re:Down the Golang Nil Rabbit Hole by pcl on Tuesday March 30, 2021 @04:58AM (#26632446)
I struggle with context.Context as well. It really comes across as a design cop-out.

The language makes a big deal about memory management and data structure ownership, and as a result it’s sorta a pain to send stuff around on the stack, both at initialization and runtime. As a casual golang user, the context mess feels like an escape hatch to help with those problems. I’d prefer some GC compromises built into the language itself instead.

Re:Down the Golang Nil Rabbit Hole by throwaway894345 on Tuesday March 30, 2021 @10:15AM (#26634828)
I'm confused about how a "GC compromise" would help with having to pass things around the stack. It sounds like you just want a request-scoped global or something, but I don't know what that has to do with the GC?

FWIW, I think request-scoped globals are terrible and is among the many things I hated about Flask. I think generics will largely solve this problem by allowing us to write generic middleware with a type-safe context and probably even functions to compose the middleware and automatically propagate the context variables.

Re:Down the Golang Nil Rabbit Hole by pcl on Tuesday March 30, 2021 @04:28PM (#26640004)
context.Context is used in a number of situations, including for a "don't call it a request-scoped global". And whatever, web frameworks always end up with one of those. Would love to see languages do better, but ok.

My concern with its use in golang is that so much seems to get jammed into it. My experience with it is that it ends up being a holding pen for all the things that other systems do with initialization-time dependency injection / resource lookup sorts of things, but replaced with "oh just pass `ctx` as an argument everywhere".

From what I've seen of Go, it looks like that happens because of the state management idiomatic constraints of the language, which in turn seem GC-related.

In a garbage-collected language, I'd put a bunch of context-y things into some sort of request executor datastructure, the web framework would manage the initialization and retention of that, and request handlers would operate against those intialized data structures. That doesn't line up all that well with idiomatic Go, and so we end up needing to hide those sorts of things somewhere else. Enter context-abuse.

Of course, there are plenty of use cases where those sorts of patterns aren't relevant. Right tool for the job and whatnot.

Again, I'm a casual golang user at best -- I bet I'm missing some newer / better approaches, and would love to learn more about what they are.

Re:Down the Golang Nil Rabbit Hole by shp0ngle on Wednesday March 31, 2021 @12:31AM (#26643544)
Go has GC? I don't understand your message that much :)
Re:Down the Golang Nil Rabbit Hole by pcl on Wednesday March 31, 2021 @06:20AM (#26645481)
Yeah but my experience with idiomatic go has been heavily value-biased, and a bunch of useful patterns for state management tend to lend themselves more towards a reference-oriented system. In my experience, this is outside the happy path, but necessary, and so people end up jamming all sorts of things into context.Context to get around the idiomatic paths.
Re:Down the Golang Nil Rabbit Hole by saturn_vk on Tuesday March 30, 2021 @06:33AM (#26632999)
My belief is that a) context should never have had a dual role of being able to hold arbitrary data (just add a map[interface{}]interface{} to the *http.Request ...) and b) it should not have existed as a means of work cancellation. Right now, one is forced to have a context argument just so that it can be passed from some upper level to a lower level that actually needs it, even if your function itself doesn't really need it. Since goroutines are built into the language, a means of cancelling them should've been built in as well.
Re:Down the Golang Nil Rabbit Hole by kortex on Tuesday March 30, 2021 @08:42AM (#26633897)
But how do you cancel something without a handle to that something?

What (I think) you are suggesting implies globally scoped, mutable variables that affect control flow dramatically. Sounds really hard to reason about and very munch anti-Go-explicitness. Context is the handle by which an outer context holds on to the nested ones.

The only non-bonkers way to have a handle like that would be to give every function a getContext function to query some kind of context tree and signal parent/children. This is totally against Go explicitness as well.

Maybe something like Scala's implicits to sugar it, but that's another not-go-looking pattern (although it would be nice)

Re:Down the Golang Nil Rabbit Hole by bbatha on Tuesday March 30, 2021 @09:10AM (#26634148)
The go statement could return a handle to the go routine. There can be a cancel magic function that cancels the go routine. You can regain the task local storage by using this handle as a key into a map of task data. This would have been a breaking change particularly in CGo threads but it’s a vastly simpler model for the user.
Re:Down the Golang Nil Rabbit Hole by saturn_vk on Tuesday March 30, 2021 @03:31PM (#26639242)
This is just hypothetical, but the go statement could be an expression that returns a cancel function. From there, the sky is the limit. And in the other side, have a runtime.Done function that returns a channel that will need closed when the said cancel function is called. There are a bunch of other runtime functions that are goroutine specific already, so there is precedent
Re:Down the Golang Nil Rabbit Hole by skrtskrt on Tuesday March 30, 2021 @10:37AM (#26635077)
I hated Go templating until I really dug into it and started loving how flexible and not-OOP it is.

It makes me want to port it to Rust, since so many of the major Rust templating libraries are Jinja copies or Handlebars copies.

Re:Down the Golang Nil Rabbit Hole by specialist on Tuesday March 30, 2021 @08:18AM (#26633726)
What would you call fixing #2? "Loop's vars are const within loop's scope?"
Re:Down the Golang Nil Rabbit Hole by shp0ngle on Tuesday March 30, 2021 @08:50AM (#26633962)
No, it can just use a different variable each loop turn?

Go authors themselves admit it might have been a design mistake, but is unfixable without breaking compatibility.

https://golang.org/doc/faq#closures_and_goroutines

> This behavior of the language, not defining a new variable for each iteration, may have been a mistake in retrospect. It may be addressed in a later version but, for compatibility, cannot change in Go version 1.

Re:Down the Golang Nil Rabbit Hole by throwaway894345 on Tuesday March 30, 2021 @10:18AM (#26634864)
> No, it can just use a different variable each loop turn?

How does that work? The call-frame needs to be of fixed-size, but it's rarely knowable at compile time how many iterations a loop will perform. Unless we begin heap-allocating loop variables or treat each loop iteration as a function call (both of which will kill performance, I think), I don't see how this could work?

Re:Down the Golang Nil Rabbit Hole by morelisp on Tuesday March 30, 2021 @12:36PM (#26636791)
You seem to have some fundamental misunderstanding about what costs memory. As the documentation says, the following works fine:

    for _, v := range values {
        v := v // create a new 'v'.
        go func() {
            fmt.Println(v)
            done <- true
        }()
    }
The scope of the variable has no effect on how many values may or may not be allocated.
Re:Down the Golang Nil Rabbit Hole by throwaway894345 on Tuesday March 30, 2021 @12:55PM (#26637076)
> You seem to have some fundamental misunderstanding about what costs memory.

Probably, hence my "How does that work?" question.

I don't doubt your example--I know it works, but I'm struggling to understand how it manages to work, since (as I understand it) the closure presumably captures the environment, which presumably means taking the address to the new `v` which presumably is the same address for each loop iteration?

Re:Down the Golang Nil Rabbit Hole by morelisp on Tuesday March 30, 2021 @01:32PM (#26637595)
https://en.wikipedia.org/wiki/Funarg_problem

Whether a loop is involved is irrelevant, it comes up any time a value referenced a closure might outlast the environment which created the closure. That can happen in loops but also if conditions or normal function calls. The value might be promoted to the heap, but in trivial cases like the above it could be copied directly on the new goroutine's stack. Or if escape analysis says the closure can't outlast the environment, it will use the same stack.

Re:Down the Golang Nil Rabbit Hole by throwaway894345 on Tuesday March 30, 2021 @01:48PM (#26637835)
That makes sense, so most likely Go's compiler is promoting the new variable to the heap (I doubt it's smart enough to recognize that the new `v` is only used by the closure which in turn is only used by the new goroutine and thus safe to allocate `v` in the new goroutine's stack). In any case, thanks for the link, I didn't know there was a formal name for the problem.
Re:Down the Golang Nil Rabbit Hole by morelisp on Tuesday March 30, 2021 @02:12PM (#26638158)
So again: Allocations of regions of memory representing values, not variables, are what does or does not get promoted. If `values` is an array of strings, do you think the following makes a new string on the heap (that is, a new 16 byte heap allocation, internally pointing to the same variable-sized region of memory)?

   for _, v := range values {
        go func(v string) {
            fmt.Println(v)
            done <- true
        }(v)
    }
No, the 16 bytes will be copied onto the goroutine's stack.

Do you think it's smart enough to do the trivial transformation of the first into the second?

Re:Down the Golang Nil Rabbit Hole by throwaway894345 on Tuesday March 30, 2021 @05:39PM (#26640895)
> So again: Allocations of regions of memory representing values, not variables, are what does or does not get promoted.

Right, I understand the difference between a variable and a region of memory.

> If `values` is an array of strings, do you think the following makes a new string on the heap (that is, a new 16 byte heap allocation, internally pointing to the same variable-sized region of memory)?

No.

> Do you think it's smart enough to do the trivial transformation of the first into the second?

I guess I'm surprised that the semantics for the `v := v` in a loop thing are "this will get allocated somewhere else such that it's guaranteed not to be stomped on by another loop iteration". For example:

    for _, v := range []int{2, 4, 6} {
        v := v
        mut := sync.Mutex{}
        go func() {
            mut.Lock()
            fmt.Println(v)
            mut.Unlock()
            done <- true
        }()
        go func() {
            mut.Lock()
            v = 1
            mut.Unlock()
            done <- true
        }()
    }
If it allocates `v` on the goroutines' stacks, then it won't catch the mutation, so presumably it allocates on the heap at least in that case. In whatever case, the semantics are surprising to me.