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.3k Golang : How to convert a number to words
+7.2k Golang : File system scanning
+4.6k Mac OSX : Get disk partitions' size, type and name
+14.8k Golang : Basic authentication with .htpasswd file
+19.2k Golang : Execute shell command
+12.2k Golang : calculate elapsed run time
+8.6k Golang : Combine slices but preserve order example
+5.7k Golang : Find change in a combination of coins example
+9.7k Golang : Resumable upload to Google Drive(RESTful) example
+6.1k Golang : Grab news article text and use NLP to get each paragraph's sentences
+10.8k Golang : Get UDP client IP address and differentiate clients by port number
+9.4k Golang : Convert(cast) string to int64