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
+22.1k Generate checksum for a file in Go
+40.5k Golang : How to check if a string contains another sub-string?
+16.3k Golang : Set up source IP address before making HTTP request
+13.2k Golang : Activate web camera and broadcast out base64 encoded images
+21.2k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+5.9k Apt-get to install and uninstall Golang
+11.2k Golang : Display a text file line by line with line number example
+7.7k Golang : How To Use Panic and Recover
+7.3k Golang : Generate human readable password
+16.6k Golang : Get number of CPU cores
+10.9k Golang : Characters limiter example