Home >Backend Development >Golang >How to Efficiently Buffer Variable-Length Data in Golang?
Buffering Variable-Length Data in Golang
In a scenario where you require a buffer to handle incoming data of variable length, the approach presented in the question uses a fixed-size buffer, potentially leading to inefficient reads. Consider the following improved solution:
<code class="go">import ( "bytes" "fmt" "io" ) func readVariableLengthData(conn io.Reader) ([]byte, error) { buf := new(bytes.Buffer) if _, err := io.Copy(buf, conn); err != nil { return nil, err } return buf.Bytes(), nil }</code>
This solution utilizes a bytes.Buffer, which allows for dynamic growth of the buffer as needed. Here's how this improved approach works:
By using this revised approach, you can handle variable-length data streams gracefully without wasting memory on unused buffer space.
The above is the detailed content of How to Efficiently Buffer Variable-Length Data in Golang?. For more information, please follow other related articles on the PHP Chinese website!