Golang
Data Types and Variables
Zero Values
In Golang, every variable declared without an explicit initialization automatically receives a default value known as its zero value. These zero values differ based on the variable's data type. Understanding how zero values work is essential for building robust Go applications and can help prevent common programming errors.
When a variable is declared without an initialization value, Go assigns the following defaults:
- For a boolean (
bool
), the zero value isfalse
. - For an integer (
int
), the zero value is0
. - For a floating-point number (e.g.,
float64
), the zero value is0.00
. - For a string (
string
), the zero value is an empty string (""
).
For more complex types—such as pointers, functions, slices, maps, channels, and interfaces—the zero value is nil
. These topics will be discussed in more detail in later sections.
Note
Zero values are not only default assignments; they also help in determining whether a variable has been explicitly initialized. This behavior is especially useful when using conditional checks or when passing variables to functions.
Integer Example
Consider the declaration of an integer variable without initialization. The following code snippet demonstrates this concept:
package main
import "fmt"
func main() {
var i int
fmt.Printf("%d", i)
}
When you run this code:
>>> go run main.go
The output will be:
0
This output occurs because 0
is the default zero value for an integer variable.
Floating-Point Example
Similarly, here is an example of a floating-point variable (float64
) that hasn't been explicitly initialized:
package main
import "fmt"
func main() {
var fl float64
fmt.Printf("%.2f", fl)
}
When you execute the code:
>>> go run main.go
The resulting output is:
0.00
This output confirms that the default zero value for a float64
variable is 0.00
.
By understanding zero values, you can write more reliable and predictable Go applications. Experiment with these examples and observe how Golang automatically assigns default values to declared variables. For more detailed insights into Go's default behavior and variable initialization, consider exploring Go's Official Documentation.
That's all for now. Happy coding!
Watch Video
Watch video content
Practice Lab
Practice lab