←back to thread

Go subtleties

(harrisoncramer.me)
234 points darccio | 3 comments | | HN request time: 0.623s | source
Show context
ignoramous ◴[] No.45666915[source]

  The time.After function creates a channel that will be sent a message after x seconds.
Or, will it? https://github.com/golang/go/issues/24595

  ... even though the value is nil, the type of the variable is a non-nil interface... Go "boxes" that value in an interface, which is not nil. This can really bite you if you return interfaces from functions
Bit me when I was noob. These days, I fail build if ireturn fails.

go install github.com/butuzov/ireturn/cmd/ireturn@latest ireturn ./...

  Go 1.25 introduced a waitgroup.Go function that lets you add Go routines to a waitgroup more easily.
sync.WaitGroup(n) panics if Add(x) is called after a Done(-1) & n isn't now zero. Unsure if WaitGroups and easy belong in the same sentence. May be they do, but I'd rather reimplement Java's CountDownLatch & CyclicBarrier APIs in Go instead.

  When you embed structs, you also implicitly promote any methods they contain ... Say, for instance, you embed a time.Time struct onto a JSON response field and try to marshal that parent ... Since the time.Time method has a MarshalJSON() method, the compiler will run that over the regular marshalling behavior
#£@&+!
replies(1): >>45667248 #
1. dontlaugh ◴[] No.45667248[source]
waitgroup's inadequacy is why I end up using structured concurrency like https://github.com/sourcegraph/conc
replies(1): >>45668127 #
2. pcthrowaway ◴[] No.45668127[source]
The last release of this was 2.5 years ago, and it's pre-1.0... is this really ready for production use?

Genuinely asking, I'm relatively new to Golang and would love to have a better sense of what parts of the ecosystem are worth learning about.

replies(1): >>45672723 #
3. hellcow ◴[] No.45672723[source]
I used it in production for lots of systems, so yes. It’s pretty great!

That said 2.5 years later there’s been many improvements to the stdlib (like waitGroup.Go) such that I no longer feel the need for it going forward.