How to Read Multi-Line Data Delimited by CRLF in Go?

Mary-Kate Olsen
Release: 2024-10-26 20:09:29
Original
112 people have browsed it

How to Read Multi-Line Data Delimited by CRLF in Go?

Reading Multi-Line Data Delimited by CRLF in Go

When working with protocols that transmit multi-line messages, it becomes necessary to handle line-separated data efficiently. In this case, the question arises about how to read data from a buffer until the delimiter character sequence CRLF occurs.

Using a Custom bufio.SplitFunc

To address this challenge, one solution is to define a custom bufio.SplitFunc that recognizes the CRLF sequence as a line termination. Here's an example implementation:

<code class="go">import (
    "bufio"
    "bytes"
)

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
}

func dropCR(data []byte) []byte {
    if len(data) > 0 && data[len(data)-1] == '\r' {
        return data[0 : len(data)-1]
    }
    return data
}</code>
Copy after login

This ScanCRLF function can be used to split data based on CRLF delimiters.

Wrapping Reader with Scanner

With the custom ScanCRLF function, you can wrap the original io.Reader with a bufio.Scanner to read data more efficiently:

<code class="go">scanner := bufio.NewScanner(r)
scanner.Split(ScanCRLF)

// Read chunks of data until EOF
for scanner.Scan() {
    fmt.Printf("%s\n", scanner.Text())
}

if err := scanner.Err(); err != nil {
    fmt.Printf("Invalid input: %s", err)
}</code>
Copy after login

Handling Unexpected Byte Counter

The alternative suggestion of reading a specific number of bytes based on a byte counter can be unreliable due to potential errors in the counter itself. Unexpected or incorrect values in the counter can lead to data corruption or truncated messages.

Therefore, the preferred approach is to use a state machine-like mechanism to parse the protocol, including the command and the expected body length, and to verify the actual body length against this expected value. This ensures data integrity and robustness.

The above is the detailed content of How to Read Multi-Line Data Delimited by CRLF in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!