Variables
Statically typed
As a statically typed language, Go knows what the types of your values are even before your program runs. Functions expect their arguments to be of a particular type and their return values to have a type as well.
Go is statically typed. If you use the wrong type of value in the wrong place, Go will let you know.
A value’s type can be determined by passing it to the TypeOf function of the reflect package.
// output
string
float64
int
bool
Declaring Variables
Variables are pieces of storage that contain values in Go. Variable declarations allow you to name variables. Use the var keyword followed by the variable’s name and the types of values it will hold.
var count int
var total, amount float64
var name string
Once you declare a variable, you can assign any value of that type.
count = 100
name = "Go"
Multiple variables can be assigned values in the same statement. Put the variable names on the left side of the = and the values on the right side, separated by commas.
total, amount = 100.00, 50.00
You can declare variables and assign them values on the same line if you know what their value will be beforehand.
var count int = 10
The variable type can usually be omitted from the declaration if you assign a value to the variable at the same time it is declared.
var count = 10
Contants
You can also declare a constant variable in the following way. You cannot modify the value of a constant variable in runtime.
const MAX_VAL = 500000000
Zero Values
When you declare a variable without assigning it a value, it will contain the zero value of its type. The zero value for numeric types is 0, for string variables is an empty string, and for bool variables is false.
var name string
var age int
var adult bool
var height float32
func main() {
fmt.Println(name) // ""
fmt.Println(age) // 0
fmt.Println(adult) // false
fmt.Println(height) // 0
}
Short Variable Declarations
It’s more common to use a short variable declaration if you know what a variable’s initial value will be right away. You can use := instead of declaring the variable’s type and assigning it later with =.
count := 10
Naming Rules
The names of variables, functions, and types in Go follow a simple set of rules.
- Names must begin with a letter, and they can contain any number of letters or numbers.
- A variable, function, or type named with a capital letter is considered exported and can be accessed from other packages.
- Variables/functions/types with lowercase names are considered unexported and can only be accessed within the current package.
- Variables, functions, and types should be named in camel case.
- Only variables, functions or types whose names begin with a capital letter are considered exported, accessible from package outside the current package.
Runes
A string represents a whole series of text characters, but a rune represents a single character.
Rune literals are written with single quotation marks (‘), while string literals are surrounded by double quotation marks (“).
If you pass a rune to fmt.Println, you will see that numeric code in the output, not the original rune.