2

I am trying to use Buffer package and copy the following code from Buffer documentation.

package main

import (
    "bytes"
    "fmt"
    "os"
)

func main() {
    var b bytes.Buffer // A Buffer needs no initialization.
    b.Write([]byte("Hello "))
    fmt.Fprintf(&b, "world!")
    b.WriteTo(os.Stdout)
}

Why do Buffer here, not to be initialize?

1

3 Answers 3

4

As you can see here Buffer consists just of some ints, the buf slice and some arrays. All of them need no initialization, since go has zero values.

You can read more about slices and arrays and how they work here.

Sign up to request clarification or add additional context in comments.

5 Comments

why I have to pass &b address in function call fmt.Fprintf(&b, "world!"). When I watch Fprintf documentation, it does not expect a pointer.
fmt.Fprintf expects an io.Writer as first parameter. (*Buffer).Write is the method on the pointer, so *Buffer is an io.Writer while Buffer is not.
Can you please make your own example
see play.golang.org/p/dHgvZPNomJ If you uncomment line 14 you will get a compiler error, since the Write method is defined on the pointer to the Buffer not on the Buffer itself. Lookup how interfaces work in Go and how pointers work (golang.org/ref/spec#Method_expressions)
@dave-cheney don't understand your edit: runeBytes [utf8.UTFMax]byte and bootstrap [64]byte are clearly arrays to me
2

It is initialized. When you do not specifically initialize a variable, go will initialize it to its zero value. That means all the internal fields of a bytes.Buffer gets the value 0, or similar for the relevant types (e.g. nil for pointers).

The authors then implemented bytes.Buffer so all values being 0 is a meaningful starting point(It means an empty buffer), so programmers doesn't need to explicitly initialize it in order to start using a Buffer.

7 Comments

This is a common idiom in Go. Because everything is initialized to zero new types can be made aware of that and treat the zero value as meaningful.
@Innominate not everything in Go is initialized: maps have no zero type, also pointers are nil by default.
@metakeule nil is a valid pointer value (it's the zero value for pointers), and maps are pointers, so they are technically initialized to their zero value.
@weberc2 Yes, but to use a map you will have to initialize it (you act on maps as if they were no pointers as with slices).
@weberc2 Ok, I get your point now. My point was to help a newbee not making wrong assumptions... ;-)
|
0

This due to the fact that when you call the Fprintf method, the bytes.Buffer.Write method is implicitely called, and as per the doc:

Write appends the contents of p to the buffer, growing the buffer as needed.

If you look at the source code, Write calls the grow function: func (b *Buffer) grow(n int) int.

This function recognizes that the buffer is empty, because it assumes that an empty buffer has 0 values for its internal fields, which is actually how a bytes.Buffer structure is initialized by default, just like every structure in go.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.