In Go, bufio provides a convenient way to read and write data in a buffered manner. However, when dealing with messages separated by carriage return line feed (CRLF) delimiters, the default behavior of bufio.ReadLine might not suffice. This article explores an alternative approach to read messages with CRLF delimiters using bufio.
To read messages with CRLF delimiters, we can utilize bufio.Scanner in conjunction with a custom scanning function. The SplitFunc method allows us to define a custom function that determines the boundaries of each message.
<code class="go">func ScanCRLF(data []byte, atEOF bool) (advance int, token []byte, err error) { if atEOF && len(data) == 0 { return 0, nil, nil } if i := bytes.Index(data, []byte{'\r', '\n'}); i >= 0 { // We have a full newline-terminated line. return i + 2, dropCR(data[0:i]), nil } // If we're at EOF, we have a final, non-terminated line. Return it. if atEOF { return len(data), dropCR(data), nil } // Request more data. return 0, nil, nil }</code>
In the custom scanning function, we search for CRLF delimiters and return the message accordingly. The dropCR function ensures that any trailing r is removed from the message.
Now, we can wrap our reader with the custom scanner:
<code class="go">scanner := bufio.NewScanner(this.reader) scanner.Split(ScanCRLF)</code>
By calling Scan, we can iterate through the messages in the reader:
<code class="go">for scanner.Scan() { fmt.Printf("%s\n", scanner.Text()) }</code>
An alternative approach is to read a specific number of bytes as specified in the message header. However, this method is prone to inconsistencies and may lead to unexpected behavior if the expected byte count is incorrect or if there is additional data in the reader buffer.
To use this approach, we can first read the header line to get the expected number of bytes:
<code class="go">res, err := this.reader.ReadLine('\n')</code>
Then, we can create a limited reader with the expected number of bytes:
<code class="go">nr_of_bytes := // read_number_of_butes_somehow(this.reader) limitedReader := io.LimitReader(this.reader, int64(nr_of_bytes))</code>
Finally, we can read the message body from the limited reader:
<code class="go">buf := make([]byte, nr_of_bytes) limitedReader.Read(buf)</code>
However, this approach has its limitations and it's generally recommended to rely on a message-based approach using CRLF delimiters.
The above is the detailed content of How to Read CRLF-Delimited Messages in Go with Bufi. For more information, please follow other related articles on the PHP Chinese website!