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
+5.4k PHP : Fix Call to undefined function curl_init() error
+16.4k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+17.9k Golang : How to log each HTTP request to your web server?
+14.2k Golang : How to convert a number to words
+12.5k Golang : Remove or trim extra comma from CSV
+11.1k Golang : Intercept and process UNIX signals example
+21.1k Golang : How to get time zone and load different time zone?
+5.6k List of Golang XML tutorials
+22.6k Golang : untar or extract tar ball archive example
+16.7k Golang : Set up source IP address before making HTTP request
+13.9k Golang : Compress and decompress file with compress/flate example
+9.9k Golang : Edge detection with Sobel method