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
+12k Golang : How to check if IP address is in range
+7.1k Golang : How to check if a string with spaces in between is numeric?
+6.8k Golang : Write multiple lines or divide string into multiple lines
+6.7k Golang : Get final balance from bit coin address example
+12.4k Golang : How to get Unix file descriptor for console and file
+11.3k Golang : How to get a user home directory path?
+11.7k Golang : Reset buffer example
+12k Golang : Normalize unicode strings for comparison purpose
+32.8k Upload multiple files with Go
+15.8k Golang : Logging with logrus
+8.1k Golang : Resolve domain name to IP4 and IP6 addresses.
+7.9k Golang : Flip coin example