1. 63
      1. 7

        rand.N working for time durations will be really handy.

        1. 4

          Really saving me from myself with the for loop footgun fix

          1. 5

            Psst, they’re not saving you from yourself — they’re saving you from the footgun.

          2. 1

            I’m happy with all the library improvements. And changing how for works to avoid dumb problems is great as well.

            But:

            Go 1.22 includes a preview of a language change we are considering for a future version of Go: range-over-function iterators

            maybe say that’s going too far, for a language which keeps things simple?

            1. 3

              maybe say that’s going too far, for a language which keeps things simple?

              I would love to see something as simple to use as yield in python tbh

              1. 4

                yield is super confusing!

                1. 2

                  How so? It is the easiest iterator pattern I have ever seen. The runtime implementation may be complicated, but the syntax/UX is great.

                  1. 1

                    I was just trying to see the what the highest rated stack overflow questions of all time. Turns out, “What does the yield keyword do in Python?” is number 5. I have always like yield in Python, but I think that’s a bit damning.

                    https://stackoverflow.com/questions?sort=votes

                    Top question is basically about branch prediction and 2-4 are all git questions, which also says a lot about git.

                2. 3

                  That would go against the Go philosophy. And frankly it’s not that much more complicated, just incredibly verbose and burdensome but that’s in keeping with the usual philosophy of Go. The translation is quite mechanical, something like:

                  • if the generator takes parameters return a func(func(yieldType) bool) and wrap the generator body in a func(func(yieldType) bool) {}

                  • replace every yield value by

                      if !yield(value) {
                          return
                      }
                    
                  1. 1

                    So, in practice, usage now looks like

                    if !yield(v) {
                       return
                    }
                    

                    If Go someday maybe gets some kind of error-related early return, it could look like try yield(v) or yield(v)! or whatever and that would be pretty good.

                    The other thing this really makes you want is a shorter syntax for declaring functions. But I guess in practice you don’t make as many new iterators as ones you use, so maybe it’s fine.

                    1. 2

                      Honestly, I would love javascript style arrow function expressions in Go for exactly this.

                      See https://github.com/golang/go/issues/21498#issuecomment-1132271548

                      1. 2

                        I don’t see the argument for this in Go, to be honest. func() vs () => saves you what, two characters? It’s the type annotations that take up all the space, and that wouldn’t change.

                        1. 1

                          saves you what, two characters?

                          7 when you include the return statement, as the most flagrant use cases for it are single-expression functional callbacks, and the linked comment includes obviating that much like arrow functions do in Javascript, or lambda in Python (even though it wastes much of the gain on the lambda keyword itself).

                          and that wouldn’t change.

                          It could though, similar to Rust which mandates annotations on named functions but allows leaving them out on anonymous functions. The contexts which would benefit from arrow functions generally constraint the types significantly, and since they would be internal to function bodies it would not be dissimilar to Go’s existing limited type inference on locals variable.

                          1. 1

                            I see, so this is really more analogous to rust lambdas, then? In which case this is a lot more than just different syntax. I can see the appeal of that.

                  2. 2

                    maybe say that’s going too far, for a language which keeps things simple?

                    I have to agree. This is the first change to Go that I really don’t like.