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
+6.1k Golang : Missing Subversion command
+5.7k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+17.8k How to enable MariaDB/MySQL logs ?
+8k Golang : Gomobile init produce "iphoneos" cannot be located error
+10.3k Golang : Wait and sync.WaitGroup example
+33.8k Golang : All update packages with go get command
+17.8k Golang : [json: cannot unmarshal object into Go value of type]
+8.1k Findstr command the Grep equivalent for Windows
+17.4k Golang : How to tell if a file is compressed either gzip or zip ?
+12.4k Golang : Get month name from date example
+22.3k Golang : Securing password with salt
+32.3k Golang : Convert []string to []byte examples