title: Pausing and starting a loop
body: <p>when a client connects to the server I am upgrading their connection to a websocket connection and sending them back data from a large json file. This works fine, however, when the user wants to stop, I receive the command and want to <code>pause</code> the stream of data and then when they want to <code>resume</code> the data is continued to be sent. I can't seem to latter two options to work. I have looked at channels but can't quiet seem to get how to do it. what I have is something like this</p>
  <pre class="lang-golang prettyprint-override"><code>func foo() {
      switch cmd {
      case &quot;play&quot;:
        for _, data := range arr {
           // send data
        }
      case &quot;stop&quot;:
      case &quot;resume&quot;:
  }
  </code></pre>
  <p>there are a couple things that I have tried but can't really get it the way that I want, one of them being channels, which I can't seem to get working the way that I want and the other is using a <code>break</code> of the the labeled loop, which isn't working because of scope:</p>
  <pre class="lang-golang prettyprint-override"><code>func foo() {
      var lastIndex := 0
  
      switch cmd {
      case &quot;play&quot;:
      Loop: // &lt;-- not used
        for i, data := range arr {
           lastIndex = i
           // send data
        }
      case &quot;stop&quot;:
        break Loop // &lt;-- error Loop not defined
      case &quot;resume&quot;:
      // start to send the data back from lastIndex
  }
  </code></pre>
  <p>am I on the right track above? Are channels better for this? Or is there a better way to do this?
  <a href="https://play.golang.org/p/iS1bQf2-bTU" rel="nofollow noreferrer">here is an example</a></p>