Variables and basic types
A variable is a name that holds a value in memory. Go is statically typed: every variable has a type that is fixed at compile time and cannot change later. This looks like a limitation, but it prevents a whole class of errors, because the compiler warns you before running the program if you try to add a number to a text. In this lesson you will see how to declare variables, which basic types exist and what value they have when you assign nothing.
Ways to declare
The full form uses the word var, the name and the type: var age int = 30. If the value allows the type to be deduced, you can omit it and Go infers it. Inside a function there is also the short form with colon and equals, name := "Ana", which declares and assigns at once; it is the most used in practice. Careful: the short form only works inside functions. Outside them, at package level, you must always use var. You can also declare several variables at once inside a block with parentheses.
package main
import "fmt"
var appName = "tasks" // package-level variable
func main() {
var age int = 30
var city = "Lima" // type inferred: string
price := 9.99 // short form: float64
active := true // bool
var a, b int = 1, 2
fmt.Println(appName, age, city, price, active, a+b)
}The basic types
Signed integers are int, int8, int16, int32 and int64; unsigned ones are uint, uint8 up to uint64. The int type has the size of the machine (64 bits on most current computers) and is the one you should use by default. For decimals there are float32 and float64; prefer float64. The bool type holds true or false. The string type holds immutable UTF-8 text. You will also see byte, which is another name for uint8, and rune, another name for int32 that represents a Unicode character; we will see them in week 2.
Zero value
In Go a variable declared without a value is never undefined: it gets its zero value. For numbers it is 0, for bool it is false, for string it is the empty string and for pointers, slices, maps and other reference types it is nil. This rule removes the famous uninitialized variable bugs of other languages and makes many types useful from the moment they exist.
package main
import "fmt"
func main() {
var n int
var f float64
var ok bool
var s string
fmt.Printf("%d %v %t %q\n", n, f, ok, s)
// prints: 0 0 false ""
var small int8 = 100
var big int64 = int64(small) * 1000 // explicit conversion
fmt.Println(big)
}Explicit conversions
Go never converts numeric types automatically. If you try to add an int to a float64 the compiler gives an error. You must write the conversion with the type name, like float64(x). It is more verbose, but there are no surprises. Be careful when converting from a large type to a small one or from decimal to integer: data is lost, and float64 to int drops the decimal part without rounding.
- Use := inside functions and var at package level.
- Prefer int and float64 unless you have a concrete reason.
- Every declared local variable must be used, or it does not compile.
- The underscore _ lets you discard a value on purpose.
✎ Meine Notizen
Nur in diesem Browser gespeichert. Melde dich an, um es im Konto zu behalten.0/5000 Zeichen