Hacker News new | ask | show | jobs
by TheDong 4183 days ago
Oh, hey, it's a generic package that means you lose type safety and saves you from writing roughly 10 lines of code.

Buffered channels + goroutines + WaitGroup already allow you to implement this trivially, and because channels are builtin generic, you can do it without the nasty type casts. Really, []interface{} is a terrible type to work with.

3 comments

>Oh, hey, it's a generic package that means you lose type safety and saves you from writing roughly 10 lines of code.

Or actually, it's not a generic (as in generics) package, which is why you lose type safety.

> Or actually, it's not a generic (as in generics) package, which is why you lose type safety.

Can we please skip just one opportunity to begin this endless flamewar on a HN post about Go? Just one? Please?

This topic has been beaten to death and beyond, and is not relevant to TFA at all, as the word "generic" can be used in many contexts, and it's more than clear which context OP meant.

>and is not relevant to TFA at all

How is it not relevant to TFA? This is a speficic example of the problem with not having generics!

Yes, I often find myself lost in this trivial channels + goroutines + WaitGroup combination. For my self I'd prefer cast interface{}, :-)
interface{} is different from []interface{}.

You can do a type assertion (not cast - these are different concepts) from interface{} to another type. You cannot do a type assertion on []interface{}; you have to iterate over each element and assert individually.

I agree with GP - the built-in primitives for synchronization, along with sync from the stdlib, are much cleaner once you know how to use them, and they're more idiomatic.

Is interface{} similar to type erasure on JVM?
No, it's most like "Object" in Java.