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
+15.2k Golang : Send email and SMTP configuration example
+5.1k Golang : Selection sort example
+16.5k Golang : Simple client server example
+7.8k Golang : Apply Histogram Equalization to color images
+6.1k Golang : Validate credit card example
+14.3k Golang : Loop each day of the current month example
+7.3k Golang : Implementing class(object-oriented programming style)
+4.7k PHP : Get client IP address
+11.4k Golang : How to calculate the distance between two coordinates using Haversine formula
+6.3k Golang : Create zip/ePub file without compression(use Store algorithm)
+9.5k Golang : Bubble sort example
+13.6k Golang : Get all local users and print out their home directory, description and group id