How to convert (type *bytes.Buffer) to use as []byte in argument to w.Write

Write requires a []byte (slice of bytes), and you have a *bytes.Buffer (pointer to a buffer).

You could get the data from the buffer with Buffer.Bytes() and give that to Write():

_, err = w.Write(buffer.Bytes())

…or use Buffer.WriteTo() to copy the buffer contents directly to a Writer:

_, err = buffer.WriteTo(w)

Using a bytes.Buffer is not strictly necessary. json.Marshal() returns a []byte directly:

var buf []byte

buf, err = json.Marshal(thing)

_, err = w.Write(buf)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)