Why you should avoid ioutil.ReadAll in Go

When you have an io.Reader , the most common way to read is with ioutil.ReadAll but it is not the best and most efficient way.

So here are three functions to compare the different methods to unmarshal JSON from an io.Reader

  1. With ioutil.ReadAll
func IOUtilReadAll(reader io.Reader) (map[string]interface{}, error) {
var (
m map[string]interface{}
b, _ = ioutil.ReadAll(reader)
)

return m, json.Unmarshal(b, &m)
}