title: GoLang Get String at Line N in Byte Slice
body: <p>In a personal project I am implementing a function that returns a random line from a long file. For it to work I have to create a function that returns a string at line N, a second function that creates a random number between 0 and lines in file. While I was implementing those I figured it may be more efficient to store the data in byte slices by default, rather than storing them in separate files, which have to be read at run time.</p>
  
  <p>Question: How would I go about implementing a function that returns a string at a random line of the <code>[]byte</code> representation of my file.</p>
  
  <p>My function for getting a string from a file:</p>
  
  <pre><code>func atLine(n int) (s string) {
      f, err := os.Open("./path/to/file")
      if err != nil {
          panic("Could not read file.")
      }
      defer f.Close()
      r := bufio.NewReader(f)
      for i := 1; ; i++ {
          line, _, err := r.ReadLine()
          if err != nil {
              break
          }
          if i == n {
              s = string(line[:])
              break
          }
      }
      return s
  }
  </code></pre>
  
  <p>Additional info:</p>
  
  <ul>
  <li>Lines are not longer than 50 characters at most</li>
  <li>Lines have no special characters (although a solution handling those is welcome)</li>
  <li>Number of lines in the files is known and so the same can be applied for <code>[]byte</code></li>
  </ul>