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
+2.6k Java : Get FX sentiment from website example
+11.7k Android Studio : Password input and reveal password example
+61.4k Golang : Convert HTTP Response body to string
+19.2k Golang : Underscore or snake_case to camel case example
+4.7k Javascript : How to refresh page with JQuery ?
+15.4k Golang : File path independent of Operating System
+5.2k Golang : Process non-XML/JSON formatted ASCII text file example
+12.7k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+14.5k Golang : How to reverse elements order in map ?
+9.3k JavaScript/JQuery : Detect or intercept enter key pressed example
+14.5k Golang : Read large file with bufio.Scanner cause token too long error
+17.9k Golang : Clearing slice