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
+7.9k Golang : Simple client-server HMAC authentication without SSL example
+3.7k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+11.4k Golang : Delete certain files in a directory
+9.9k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+7k Golang : Simple image viewer with Go-GTK
+7k Golang : Intercept and process UNIX signals example
+14.8k Golang : Example for DSA(Digital Signature Algorithm) package functions
+3.6k Golang : Gargish-English language translator
+7.8k Golang : Get user input until a command or receive a word to stop
+6.2k Golang : Take screen shot of browser with JQuery example
+14k Golang : Clean up null characters from input data
+9.2k Golang : Find network of an IP address