This question already has an answer here:
The following is what I want to achieve
fmt.Println(string(ioutil.ReadAll(res.Body)))
But this throws the following error.
multiple-value ioutil.ReadAll() in single-value context
I know that ioutil.ReadAll() returns the bytes and the error. But I don't want to write an extra line as follows
bytes, _ := ioutil.ReadAll(resp.Body)
Is it possible to just pass the result of ioutil.ReadAll() to fmt.Println() if don't care abut error handling in Go?
</div>
If you want it only once, it makes little sense in my opinion. However if this is a common situation and you feel that it improves code readability a lot, try something like:
// perhaps, there's a better name for this
func resultToString(b []byte, _ error) string {
return string(b)
}
// and later:
fmt.Println(resultToString(ioutil.ReadAll(res.Body)))
Unfortunately, that's a string-specific function, so for any other type you'll need to duplicate it.