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.7k Golang : GMail API create and send draft with simple upload attachment example
+13.8k Golang : Force download file example
+10.1k CodeIgniter : How to check if a session exist in PHP?
+14.7k Golang : Generate universally unique identifier(UUID) example
+6.3k Golang : Example of how to detect which type of script a word belongs to
+7.8k Golang : Take screen shot of browser with JQuery example
+5.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+9.3k Golang : Generate random integer or float number
+15.9k Golang : Defer function inside init()
+6.9k Golang : Qt splash screen with delay example
+8.8k Golang : Channels and buffered channels examples
+6.5k Golang : Shuffle strings array