title: panic: runtime error: slice bounds out of range when concurrently running as goroutine
body: <p>I'm calling a function as a goroutine and using a WaitGroup to prevent closing a shared Scanner before they all finish. The <code>myfunc()</code> function iterates over a file. I wanted to memory-map this file and share it between all of the goroutines rather than have the I/O chokepoint of reading from disk each time. I was told that this approach would work <a href="https://stackoverflow.com/a/48026498/868044">in an answer to another question.</a> However, while this function worked fine standalone, it's not working concurrently. I am receiving the error:</p>
  
  <pre><code>panic: runtime error: slice bounds out of range
  </code></pre>
  
  <p>but the error is when I call the <code>Scan()</code> method (not on a slice) which is confusing.</p>
  
  <p>Here is a MWE:</p>
  
  <pre><code>// ... package declaration; imports; yada yada
  
  // the actual Sizes map is much more meaningful, this is just for the MWE
  var Sizes = map[int]string {
      10: "Ten",
      20: "Twenty",
      30: "Thirty",
      40: "Forty",
  }
  
  type FileScanner struct {
      io.Closer
      *bufio.Scanner
  }
  
  func main() {
      // ... validate path to file stored in filePath variable
      filePath := "/path/to/file.txt"
  
      // get word list scanner to be shared between goroutines
      scanner := getScannerPtr(&amp;filePath)
  
      // call myfunc() for each param passed
      var wg sync.WaitGroup
      ch := make(chan string)
      for _, param := range os.Args[1:] {
          wg.Add(1)
          go myfunc(&amp;param, scanner, ch)
          wg.Done()
      }
  
      // print results received from channel
      for range os.Args[1:] {
          fmt.Println(&lt;-ch)  // print data received from channel ch
      }
  
      // don't close scanner until all goroutines are finished
      wg.Wait()
      defer scanner.Close()
  }
  
  func getScannerPtr(filePath *string) *FileScanner {
      f, err := os.Open(*filePath)
      if err != nil {
          fmt.Fprint(os.Stderr, "Error opening file\n")
          panic(err)
      }
      scanner := bufio.NewScanner(f)
      return &amp;FileScanner{f, scanner}
  }
  
  func myfunc(param *string, scanner *FileScanner, ch chan&lt;-string) {
      for scanner.Scan() {
          line := strings.TrimSpace(scanner.Text())
          // ... do something with line (read only)
          // ... access shared Sizes map when doing it (read only)
          ch &lt;- "some string result goes here"
      }
  }
  </code></pre>
  
  <p>I originally thought the issue was concurrent access to the shared Sizes map, but moving it inside <code>myfunc()</code> (and inefficiently redeclaring/redefining it every time) still resulted in the same error, which has to do with calling <code>Scan()</code>. I'm attempting to follow the guidance I received <a href="https://stackoverflow.com/a/48026498/868044">in this answer.</a></p>
  
  <p>Here's the full stack trace of the panic:</p>
  
  <pre><code>panic: runtime error: slice bounds out of range
  
  goroutine 6 [running]:
  bufio.(*Scanner).Scan(0xc42008a000, 0x80)
          /usr/local/go/src/bufio/scan.go:139 +0xb3e
  main.crack(0xc42004c280, 0xc42000a080, 0xc42001c0c0)
          /Users/dan/go/src/crypto_ctf_challenge/main.go:113 +0x288
  created by main.main
          /Users/dan/go/src/crypto_ctf_challenge/main.go:81 +0x1d8
  exit status 2
  </code></pre>
  
  <p>Line 81 is:</p>
  
  <pre><code>go myfunc(&amp;param, scanner, ch)
  </code></pre>
  
  <p>Line 113 is:</p>
  
  <pre><code>for scanner.Scan() {
  </code></pre>