Do I need to assign a default value to a variable in Golang?

In Golang, when a variable is declared, it is initialized to a null value, as described in the specification.

http://golang.org/ref/spec#The_zero_value

But is it good coding practice to use this property and not explicitly initialize your variable if it should be initialized with a default value.

for example in the following example

http://play.golang.org/p/Mvh_zwFkOu

package main import "fmt" type B struct { isInit bool Greeting string } func (b *B) Init() { b.isInit = true b.Greeting = "Thak you for your time" } func (b *B) IsInitialized() bool { return b.isInit } func main() { var b B if !b.IsInitialized(){ b.Init() } fmt.Println(b.Greeting) } 

the program relies on the default boolean as false.

+8
go default-value default
source share
3 answers

As everyone says, the specification is clear: all memory is initialized (zeroed). You should take advantage of this, as standard packages do. In particular, this allows you to rely on the "default constructor" for your own types and often skip the New() *T functions in favor of &T{} .

Many types in standard packages use this, some examples:

http.Client

A client is an HTTP client. Its null value (DefaultClient) is a convenient client that uses DefaultTransport.

And then you will find var DefaultClient = &Client{} declared in the package.

http.Server

The server defines the parameters for starting the HTTP server. A null value for the Server is a valid configuration.

bytes.Buffer

A buffer is a variable byte buffer with read and write methods. A zero value for the buffer is an empty buffer ready for use.

This is great because you can just make var buf bytes.Buffer and start using it. Because of this, you will also often see logical member variables that will be used in a "negative" form - for example, InsecureSkipVerify in tls.Config not called Verify , because the default behavior will not check certificates (I think I want false - or zero value was used for the desired default values).

Finally, answering your question:

But is it good coding practice to use this property and not explicitly initialize your variable if it should be initialized with a default value?

Yes it is.

+5
source share

Initialize variables to their null values โ€‹โ€‹only if you want to use short declaration syntax.

 //less verbose than ''var count int'' count := 0 empty := "" 

Otherwise, their initialization is clearly just noise. You might think that something is wrong with uninitialized variables ... and you would be right. Fortunately, there is no such thing. Zero values โ€‹โ€‹are part of the specification and they will not change suddenly.

+6
source share

When a variable is declared, it automatically contains a default value of zero or null for its type: 0 for int , 0.0 for float , false for bool , empty string for string , nil for pointer, null structure, etc.

All memory in Go is initialized!

For example: var arr [5]int in memory can be visualized as:

+---+---+---+---+ | | | | | +---+---+---+---+ 0 1 2 3

When declaring an array, each element in it is automatically initialized with a zero default value for this type, here all elements by default are 0.

Therefore, it is advisable to initialize without a default value, in other cases than situations where you explicitly want to declare a variable with a default value.

+2
source share

All Articles