Created
April 2, 2014 18:29
-
-
Save tejainece/9940151 to your computer and use it in GitHub Desktop.
Golang: io.Reader stream to string or byte slice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Nice code, better do aN err check for ReadFrom.
usable only once is because the nature of the steam
👍
nice
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
thank you that was very useful, but the problem with this code is that it is usable only once, i.e: if you read once the next time you have an empty output.