Variables
This lesson discusses how variables are used in Go.
We'll cover the following...
Introduction
A value that can be changed by a program during execution is called a variable. The general form for declaring a variable uses the keyword var as:
var identifier type
Here, identifier
is the name of the variable, and type
is the type of the variable. As discussed earlier in this chapter, type
is written after the identifier
of the variable, contrary to most older programming languages. When a variable is declared, memory in Go is initialized, which means it contains the default zero or null value depending upon its type automatically. For example, 0 for int, 0.0 for float, false for bool, empty string ("") for string, nil for pointer, zero-ed struct, and so on.
Run the following program to see how declaring a variable works.
package mainimport "fmt"func main(){var num int // Declaring an integer variablefmt.Println(num) // Printing its valuevar decision bool // Declaring a boolean variablefmt.Println(decision) // Printing its value}
You can see that in the above code, we declare a ...