2.1. Variables
Basics
Go is a statically typed language. This means that each variable gets a type on declaration which can’t be changed later.
Commonly used data types are:
intanduintfloat32andfloat64boolstringbyte(alias foruint8)errorto return errors from functions
All Go’s predeclared identifiers are defined in the builtin package.
Declaration
The short assignment statement := declares a variable and assigns a value to it.
The type of variable is inferred from the value (type inference).
| |
Output:
foo.txt 42 true
Once a variable is declared you can assign values to it with a regular assignment =.
| |
Output:
bar.csv
With the var keyword you can declare a variable without assigning a value to it.
| |
Output:
0 false
Zero values
If you declare a variable and do not assign a value it is initialized with the zero value of its type.
The zero values are:
0for numeric typesfalsefor booleans""(empty string) for strings
Last modified May 29, 2024: Merge pull request #205 from acend/renovate/actions-checkout-digest (cc47d9a)