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
+6k Golang : How to get capacity of a slice or array?
+11.8k Golang : convert(cast) string to integer value
+11.2k Golang : Generate DSA private, public key and PEM files example
+17.3k Convert JSON to CSV in Golang
+7.3k Gogland : Single File versus Go Application Run Configurations
+4.7k Unix/Linux : secure copying between servers with SCP command examples
+10.9k Golang : Intercept and process UNIX signals example
+17.7k Golang : Put UTF8 text on OpenCV video capture image frame
+14.3k Golang : How to get URL port?
+9.7k Golang : Identifying Golang HTTP client request
+8k Golang : Number guessing game with user input verification example
+5.8k Golang : Build new URL for named or registered route with Gorilla webtoolkit example