Module lessons (1/5)
Variables and types
Fundamentals of Go's type system
Understand static typing, local inference with :=, and the zero-unused-variables rule.
Go is a statically typed language: each variable has a fixed type known at compile-time. There is no silent coercion of dynamic languages, but thanks to the := operator, writing remains as light as Python.
Bid with var vs :=
There are two main ways to declare a variable:
package main
import "fmt"
// A livello di package si può usare SOLO var (o const), mai :=
var version string = "1.0"
func main() {
// 1. Forma estesa con tipo esplicito
var name string = "Ada"
// 2. Forma corta con inferenza di tipo (solo dentro una funzione)
age := 36
fmt.Println(version, name, age)
}| Syntax | Where it works | Type | When to prefer it |
|---|---|---|---|
var x T = v | Packages and functions | Explicit T | Public APIs, non-default types (int64, float32) |
var x T | Packages and functions | Explicit T | Initialization to zero value (0, "", false) |
x := v | Only inside functions | Inferred from v | 90% of local variables |
The basic types of everyday use
The standard library covers almost every scenario with four basic types, flanked by two important aliases (byte and rune):
package main
import "fmt"
func main() {
count := 42 // int (64 bit su sistemi a 64 bit)
price := 19.99 // float64 (doppia precisione)
city := "Milano" // string (sequenza immutabile di byte UTF-8)
ready := true // bool (true o false)
letter := 'À' // rune (alias di int32: un code point Unicode)
rawByte := byte('A') // byte (alias di uint8: un ottetto grezzo)
fmt.Printf("%T %T %T %T %T %T\n", count, price, city, ready, letter, rawByte)
}var blocks and multiple declarations
You can group related statements inside a var ( ... ) block or assign multiple variables on a single line:
package main
import "fmt"
var (
host = "localhost"
port = 8080
secure bool
)
func main() {
// Tipi eterogenei sulla stessa riga
user, level, active := "Marco", 3, true
fmt.Println(host, port, secure, user, level, active)
}Unused variables and shadowing error
Go treats local variables and unused import as compilation errors, not simply warnings. If you need a variable temporarily in draft form, you can explicitly discard it with the blank identifier _:
package main
import "fmt"
func main() {
x := 42
_ = x // silenzia temporaneamente l'errore di compilazione
status := "outer"
if ok := true; ok {
status := "inner" // ATTENZIONE: crea una nuova variabile locale!
fmt.Println("inside:", status)
}
fmt.Println("outside:", status) // stampa ancora "outer"
}Guided practice
Declare with var a variable name of type string with value "Ada" inside main and print it.
Show hint
The long form combines the var keyword, name, type, and assignment: var name string = "Ada".
Solution available after 3 attempts
In a single line inside main, use := to assign 36 to age and "Roma" to city, then print both.
Show hint
The multiple short declaration separates names and values with commas: age, city := 36, "Roma".
Solution available after 3 attempts
Declare role at the package level with var role string = "admin", then inside main create score := 98.5 (float64 inferred) and debug := true, silence debug with _ = debug, and print role and score.
Show hint
At package level use var role string = "admin", while inside main use := and assign _ = debug to avoid the unused variable error.
Solution available after 3 attempts
Check for understanding
Which of these declarations produces a compilation error if placed OUTSIDE a function (package level)?
// (a)
var name string = "Ada"
// (b)
age := 36
// (c)
const PI = 3.14What does this Go program print about the final value of x?
package main
import "fmt"
func main() {
x := 10
if true {
x := 99
_ = x
}
fmt.Println(x)
}