I'm new in go language. I'm trying to understand what's happening inside ioutil.ReadAll(r Reader, capacity int64) method. Inside this method there is a line like:
buf := bytes.NewBuffer(make([]byte, 0, capacity))
But the problem is inside bytes package there is a NewBuffer method with only parameter like:
func NewBuffer(buf []byte) *Buffer
I searched bytes package documentation again and again but can't find NewBuffer method with 3 parameter.
So actually from where NewBuffer(make([]byte, int, int64)) method calling?
make([]byte, 0, capacity)
returns a new byte slice ([]byte
) initialized to length 0 and capacity capacity
.
In other words, it might help you to see it on multiple lines:
capacity := 100 // or whatever
var myBytes []byte = make([]byte, 0, capacity)
buf := bytes.NewBuffer(myBytes)
Youre missing the fact that you're calling the bytes.NewBuffer()
function with a single parameter, which happens to be a method call - make([]byte, 0, capacity)
- which returns an byte slice.