title: golang - bufio read multiline until (CRLF) \r\n delimiter
body: <p>I am trying to implement my own beanstalkd client as a way of learning go. <a href="https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt" rel="noreferrer">https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt</a></p>
  
  <p>At the moment, I am using <code>bufio</code> to read in a line of data delimited by <code>\n</code>.</p>
  
  <p><code>res, err := this.reader.ReadLine('\n')</code></p>
  
  <p>This is fine for when I send a single command, and read a a single line response like: <code>INSERTED %d\r\n</code> but I find difficulties when I try to reserve a job because the job body could be multiple lines and as such, I cannot use the <code>\n</code> delimiter.</p>
  
  <p>Is there a way to read into the buffer until <code>CRLF</code>?</p>
  
  <p>e.g. when I send the <code>reserve</code> command. My expected response is as follows:</p>
  
  <pre><code>RESERVED &lt;id&gt; &lt;bytes&gt;\r\n
  &lt;data&gt;\r\n
  </code></pre>
  
  <p>But data could contain <code>\n</code>, so I need to read until the <code>\r\n</code>.</p>
  
  <p>Alternatively - is there a way of reading a specific number of bytes as specified in <code>&lt;bytes&gt;</code> in example response above?</p>
  
  <p>At the moment, I have (err handling removed):</p>
  
  <pre><code>func (this *Bean) receiveLine() (string, error) {
      res, err := this.reader.ReadString('\n')
      return res, err
  }
  
  func (this *Bean) receiveBody(numBytesToRead int) ([]byte, error) {
      res, err := this.reader.ReadString('\r\n') // What to do here to read to CRLF / up to number of expected bytes?
  
      return res, err
  }
  
  func (this *Bean) Reserve() (*Job, error) {
  
      this.send("reserve\r\n")
      res, err := this.receiveLine()
  
      var jobId uint64
      var bodylen int
      _, err = fmt.Sscanf(res, "RESERVED %d %d\r\n", &amp;jobId, &amp;bodylen)
  
      body, err := this.receiveBody(bodylen)
  
      job := new(Job)
      job.Id = jobId
      job.Body = body
  
      return job, nil
  }
  </code></pre>