package main import ( "bytes" "fmt" ) func main() { var buffer bytes.Buffer buffer.WriteString("Hello, ") buffer.WriteString("World!") fmt.Println(buffer.String()) }
package main import ( "bytes" "encoding/binary" "fmt" ) func main() { var buffer bytes.Buffer binary.Write(&buffer, binary.LittleEndian, uint16(42)) fmt.Println(buffer.Bytes()) }This code demonstrates how to use a bytes.Buffer to write binary data to memory. In this example, we use the encoding/binary package to write a uint16 value to the buffer using little-endian byte order. The buffer's raw byte data can then be retrieved using the Bytes method. Package library: Go standard library.