Golang : Convert(cast) io.Reader type to string
Problem :
You have input of type io.Reader and you need to convert the input to string type.
Solution :
First, create a buffer and use the buffer's ReadFrom()
function to read/parse the io.Reader input. Next, assign the buffer's string value to a string variable.
For example :
// convert io.Reader type to string
var msg io.Reader
...
buffer := new(bytes.Buffer)
buffer.ReadFrom(msg.Body)
str := buffer.String()
fmt.Println(str)
See full example at : https://www.socketloop.com/references/golang-net-mail-message-type-and-readmessage-function-example
References :
https://www.socketloop.com/tutorials/golang-convert-cast-bytes-to-string
https://www.socketloop.com/references/golang-net-mail-message-type-and-readmessage-function-example
See also : Golang : convert(cast) bytes to string
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+11.5k Golang : Surveillance with web camera and OpenCV
+11.4k Golang : Find age or leap age from date of birth example
+13.8k Golang : convert rune to unicode hexadecimal value and back to rune character
+5.9k Golang : Missing Subversion command
+6.5k Golang : Skip or discard items of non-interest when iterating example
+16.4k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+10.1k Golang : How to profile or log time spend on execution?
+7.8k Golang : Gomobile init produce "iphoneos" cannot be located error
+12.8k Swift : Convert (cast) Int to String ?
+22.5k Golang : Strings to lowercase and uppercase example
+14.4k Golang : Execute function at intervals or after some delay