Hacker News new | ask | show | jobs
by patcoll 4229 days ago
I've been using this library which helps with composing channels, and also has some nice implementations of useful patterns:

https://github.com/eapache/channels

Unfortunately Go's lack of generics hurts in letting me use these helpers in a way that guarantees the data types going in/out of the generic channels.

1 comments

Right. Everything gets converted to Go's "any" type, "Interface{}". At the output end you have to use a type assertion to get it back into the actual type. Note that an "Interface{}" is a pointer; you're not queuing the data, you're queuing a pointer to the data. So you're sharing data between goroutines. The sender must allocate a new variable for each send.

(This is my biggest complaint about Go. It's too easy to accidentally share data between goroutines, because so many things are implicitly references to mutable objects. Slices, for example, are references. Erlang and Rust, which also use queues heavily, avoid doing this.)