Home  >  Article  >  Backend Development  >  How to read data from a buffer until a specific delimiter, CRLF, using a custom SplitFunc in Go?

How to read data from a buffer until a specific delimiter, CRLF, using a custom SplitFunc in Go?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-26 09:48:03697browse

How to read data from a buffer until a specific delimiter, CRLF, using a custom SplitFunc in Go?

Custom Delimited Reader with CRLF

To read data from a buffer until a specific delimiter, in this case, the CRLF (carriage return, line feed) sequence, implement a custom SplitFunc for bufio.Scanner.

<code class="go">// ScanCRLF splits the input data into tokens that are terminated by CRLF.
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 {
        return i + 2, dropCR(data[0:i]), nil
    }
    if atEOF {
        return len(data), dropCR(data), nil
    }
    return 0, nil, nil
}

// dropCR drops a terminal \r from the data.
func dropCR(data []byte) []byte {
    if len(data) > 0 && data[len(data)-1] == '\r' {
        return data[0 : len(data)-1]
    }
    return data
}</code>

Wrap the reader with the custom scanner and use the Scan() function to read each line:

<code class="go">// Wrap the reader with the custom scanner
scanner := bufio.NewScanner(this.reader)
scanner.Split(ScanCRLF)

// Read each line using the scanner
for scanner.Scan() {
    // Process the line as needed...
}

// Check for any errors encountered by the scanner
if err := scanner.Err(); err != nil {
    // Log or handle the error
}</code>

Alternative: Reading Specific Number of Bytes

Another option is to read a specific number of bytes. First, you need to determine the size of the body using the protocol, e.g., by reading the "bytes" value from the response.

<code class="go">// Read the expected number of bytes
nr_of_bytes, err := this.reader.ReadNumberBytesSomeHow()
if err != nil {
    // Handle the error
}

// Allocate a buffer to hold the body
buf := make([]byte, nr_of_bytes)

// Read the body into the buffer
this.reader.Read(buf)

// Process the body as needed...</code>

However, relying on the byte counter can be risky. It's recommended to use the custom reader approach with CRLF delimiter, as it follows the protocol more closely.

The above is the detailed content of How to read data from a buffer until a specific delimiter, CRLF, using a custom SplitFunc in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn