Most active commenters

    ←back to thread

    Go subtleties

    (harrisoncramer.me)
    235 points darccio | 11 comments | | HN request time: 0.708s | source | bottom
    1. acatton ◴[] No.45667905[source]
    > The wg.Go Function

    > Go 1.25 introduced a waitgroup.Go function that lets you add Go routines to a waitgroup more easily. It takes the place of using the go keyword, [...]

    99% of the time, you don't want to use sync.WaitGroup, but rather errgroup.Group. This is basically sync.WaitGroup with error handling. It also has optional context/cancellation support. See https://pkg.go.dev/golang.org/x/sync/errgroup

    I know it's not part of the standard library, but it's part of the http://golang.org/x/ packages. TBH, golang.org/x/ is stuff that should be in the standard library but isn't, for some reason.

    replies(5): >>45668472 #>>45669297 #>>45669628 #>>45671829 #>>45672717 #
    2. blixt ◴[] No.45668472[source]
    I thought exactly the same thing. I use errgroup in practically every Go project because it does something you'd most likely do by hand otherwise, and it does it cleaner.

    I discovered it after I had already written my own utility to do exactly the same thing, and the code was almost line for line the same, which was pretty funny. But it was a great opportunity to delete some code from the repo without having to refactor anything!

    replies(1): >>45672134 #
    3. lagniappe ◴[] No.45669297[source]
    >golang.org/x/ is stuff that should be in the standard library but isn't, for some reason

    think of it as testing/staging before being merged into stable stdlib

    replies(2): >>45672531 #>>45673067 #
    4. mholt ◴[] No.45669628[source]
    The extended standard lib is pretty great, but definitely can't keep the Go compatibility promise, so it's good that it's separate.
    5. h4ck_th3_pl4n3t ◴[] No.45671829[source]
    I never used errgroup but I realize that it's essentially the same what I end up implementing anyways.

    With standard waitgroups I always move my states as a struct with something like a nested *data struct and an err property which is then pushed through the channel. But this way, my error handling is after the read instead of right at the Wait() call.

    6. giancarlostoro ◴[] No.45672134[source]
    > and the code was almost line for line the same, which was pretty funny.

    One of the core strengths of Go is that it fits the zen of Python's " There should be one-- and preferably only one --obvious way to do it" and it does this very nicely.

    7. linhns ◴[] No.45672531[source]
    I do believe it’s backwards compatibility and evolving APIs
    8. infogulch ◴[] No.45672717[source]
    Wow how did I not know of this?!

    How does it cancel in-progress goroutines when the provided context is cancelled?

    replies(1): >>45672963 #
    9. wesleyd ◴[] No.45672963[source]
    They have to all use the special context.
    replies(1): >>45676231 #
    10. CamouflagedKiwi ◴[] No.45673067[source]
    Except that it's a little bit too convenient, and highly useful things like errgroup stay there instead of having been adopted into the stdlib.
    11. lowmagnet ◴[] No.45676231{3}[source]
    They just need to be context aware, or call context-aware things.