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
+9.1k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+17.8k Golang : Check if a directory exist or not
+16.7k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+19.2k Golang : How to count the number of repeated characters in a string?
+16.8k Golang : Get number of CPU cores
+8.7k Golang : Sort lines of text example
+8.5k Golang : Combine slices but preserve order example
+8.9k Android Studio : Indicate progression with ProgressBar example
+16.1k Golang : Find out mime type from bytes in buffer
+14.9k Golang : Save(pipe) HTTP response into a file
+4.9k Golang : Display packages names during compilation
+4.6k PHP : Extract part of a string starting from the middle