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
+6k Javascript : How to replace HTML inside <div>?
+5.3k Responsive Google Adsense
+5.6k Unix/Linux : How to archive and compress entire directory ?
+7.5k Golang : Not able to grep log.Println() output
+6.5k Golang : How to get capacity of a slice or array?
+8.6k Golang : Convert word to its plural form example
+27.6k Golang : Convert CSV data to JSON format and save to file
+11.5k Golang : Post data with url.Values{}
+4.8k Linux : sudo yum updates not working
+11.4k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+17.8k Golang : Parse date string and convert to dd-mm-yyyy format
+7.3k Nginx : How to block user agent ?