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
+12.1k Golang : Convert a rune to unicode style string \u
+4.8k JavaScript: Add marker function on Google Map
+13.7k Golang : Strings comparison
+14.3k Golang : Convert IP version 6 address to integer or decimal number
+5.8k List of Golang XML tutorials
+39.8k Golang : Remove dashes(or any character) from string
+8.8k Golang : Progress bar with ∎ character
+15.6k Golang : Get all local users and print out their home directory, description and group id
+11.2k Golang : Generate random elements without repetition or duplicate
+10.8k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+4.8k Unix/Linux : How to pipe/save output of a command to file?
+20.6k nginx: [emerg] unknown directive "passenger_enabled"