Skip to content

Instantly share code, notes, and snippets.

@tejainece
Created April 2, 2014 18:29
Show Gist options
  • Save tejainece/9940151 to your computer and use it in GitHub Desktop.
Save tejainece/9940151 to your computer and use it in GitHub Desktop.
Golang: io.Reader stream to string or byte slice
import "bytes"
func StreamToByte(stream io.Reader) []byte {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.Bytes()
}
func StreamToString(stream io.Reader) string {
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.String()
}
@mayu899
Copy link

mayu899 commented Mar 22, 2019

Nice code, better do aN err check for ReadFrom.
usable only once is because the nature of the steam

@luillyfe
Copy link

luillyfe commented Dec 6, 2020

👍

@hybnew
Copy link

hybnew commented Jan 13, 2021

nice

@amanbolat
Copy link

io.Copy is much faster than using ReadFrom method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment