// ReadLineBytes read till '\n' is found or encounter error. The returned line // does not include ending '\r\n' or '\n'. Returns err != nil if and only if // len(line) == 0. Note the returned byte should not be used for append and // maybe overwritten by next I/O operation. Copied code of readLineSlice from // $GOROOT/src/pkg/net/textproto/reader.go func ReadLineSlice(r *bufio.Reader) (line []byte, err error) { for { l, more, err := r.ReadLine() if err != nil { return nil, err } // Avoid the copy if the first call produced a full line. if line == nil && !more { return l, nil } line = append(line, l...) if !more { break } } return line, nil }