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
+14.5k Golang : How to convert a number to words
+6.6k PHP : Shuffle to display different content or advertisement
+15.4k nginx: [emerg] unknown directive "ssl"
+9.5k Golang : How to get username from email address
+18.1k Golang : Get all upper case or lower case characters from string example
+10.8k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+10k Golang : Turn string or text file into slice example
+9.1k Golang : automatically figure out array length(size) with three dots
+13.8k Golang : Activate web camera and broadcast out base64 encoded images
+18.8k Unmarshal/Load CSV record into struct in Go
+7.5k Golang : Example of custom handler for Gorilla's Path usage.
+12.5k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter