Most strings in Go are dealt with as byte slices []byte
. Here's some further information from the Go blog about this.
The bytes.Buffer
type implements the io.Writer
interface and provides some convenience methods for working with a slice of bytes.
You can create a empty bytes buffer:
buf := new(bytes.Buffer)
And then you can write to it:
buf.Write([]byte("Hello, World!"))
Or, you could write this:
var buf bytes.Buffer
&buf.Write([]byte("Hello, World!"))
In these examples, we are casting the strings to []byte
so we can use the Write
method. You can also use strings directly with the WriteString
method.
var buf bytes.Buffer
buf.WriteString("Hello, World!")
And then we can get the string with the String
method so we can print it out:
fmt.Println(buf.String())
You can use the fmt
package to print out the value, but you can also write the contents of a bytes.Buffer
to an io.Writer
with the WriteTo
method (here we're using os.Stdout
as our io.Writer
).
buf.WriteTo(os.Stdout)