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
+4.9k Golang : Launching your executable inside a console under Linux
+7.1k Golang : Oanda bot with Telegram and RSI example
+12.9k Golang : Get uploaded file name or access uploaded files
+7.8k Golang : GMail API create and send draft with simple upload attachment example
+7.8k Golang : Heap sort example
+30.9k Golang : Convert []string to []byte examples
+5k Golang : How to write backslash in string?
+14.7k Golang : Read a file line by line
+14.1k Golang : Validate hostname
+6.2k Golang : Trim everything onward after a word
+19k Golang : Secure(TLS) connection between server and client
+10.3k Golang : Display a text file line by line with line number example