-rob
> If the data is only going to be read, could broadcasting pointers
> instead of values save some memory alloc/gc depending on subscriber-
> size/data-size/broadcast-frequency?
> Is this style discouraged?
Sending pointers on a channel is fine. Whether it makes sense depends
on the size of the value, as you say. It would be pointless to send a
pointer rather than an int. It might well be more efficient to send a
pointer rather than a 100-byte structure.
Ian
Hello Everybody,I would like to know if there is a way to set up one to many channels.. i.e. Can I have a channel which would broadcast its input on a bunch of channels? The solution I currently use is have a goroutine which listens on this channel and broadcasts it on a collection of channels . If I want to another channel which should be broadcasted on.. then I would add this extra channel to the collection of channels. Is this a good of doing this kind of broadcast? Is there a better way of doing this?
Thanks,Sunil.
> The reason I asked if it is discouraged is because of the absence of a
> const-style type-constraint.
>
> In that case there won't be an error thrown if the code on the
> subscriber-side modifies the data passed as a pointer(library design
> for instance)
That is true.
> Could the compiler do this optimization if it knows that the data sent
> is not modified?
> This could help the GC a lot..
> Or would that lead to too much automagic-code/over-engineering?
The compiler could do that in principle in some cases, but I think the
context would be so limited that it's unlikely that this optimization
would ever be implemented. It could only be done if all the relevant
code were in the same package. The compiler would have to be able to
prove that it could see all possible sends and receives on the channel
in question. Without thinking about it too hard, I suspect that there
would be many cases where it would seem that the optimization would
apply but where the compiler could not quite prove that it would apply.
Ian
Thanks for the reply. What I'm trying to do is to make a web socket server where all clients are sent broadcast messages. When the client connects, a channel is creates and registered in a global list of client channels. When a client disconnects, the channel gets closed. The broadcaster does not know and crashes.
Is it possible to gracefully skip channels that are closed, or do I need to create a registry of clients that have their own IDs mapped to a channel so that I can prune them? I ended up doing it this way and I realize that leaving closed channels in the list is memory bloat over time, but it would have saved a lot of time on this throw away program to just loop over a slice of channels.
Thanks again for the reply.
Perfect. Thanks!