Go’s Type System
Go is a statically typed language, which means the compiler must know the types of all variables to build a program. The compiler looks for a special variable declaration signature and allocates enough memory to store its value.
func main() {
var n int
n = 42
}
By default, Go initializes the memory with the zero value corresponding to its type. In the preceding example, we declare `n`, which has an initial value of 0. We later assign a new value of 42.
As its name suggests, a variable can change its value, but only as long as its type remains the same. If you try to assign a value with a different type or re-declare a variable, the compiler complains with an appropriate error message.
If we appended a line with n = "Hello"
to the last code example, the program wouldn't compile, and it would return the following error message: cannot use "Hello" (type untyped string) as type int in assignment.
You...