title: Sharing a slice across a few different goroutines
body: <p>Given that I have an slice of structs of type User</p>
  
  <pre><code>Users := make([]User)
  </code></pre>
  
  <p>I am listening for TCP connections, and when a user connects, I'm adding an new user to this slice.</p>
  
  <p>The way I've done this, is by setting up a NewUsers channel</p>
  
  <pre><code>NewUsers := make(chan User)
  </code></pre>
  
  <p>Upon new TCP connects, a User gets sent to this channel, and a central function waits for a User to arrive to add it to the Users slice.</p>
  
  <p>But now I would like multiple subsystems (packages/functions) to use this list of Users. One function might simply want to receive a list of users, while a different function might want to broadcast messages to every user, or just users matching a certain condition.</p>
  
  <p>How do multiple functions (which are possibly executed from different goroutines) safely access the list of users. I see two possible ways:</p>
  
  <ol>
  <li>Every subsystem that needs access to this list needs their own AddUser channel and maintain their own slice of users and something needs to broadcast new users to every one of these channels.</li>
  <li>Block access with a Mutex</li>
  </ol>
  
  <p>Option 1 seems very convoluted and would generate a fair bit of duplication, but my understanding that Mutexes are best to be avoided if you try to stick to the "Share Memory By Communicating" mantra.</p>