Skip to main content
Module 1 · Lesson 1 of 51/50 in the course~10 min
Module lessons (1/5)

Variables and types

Warm-Up Stage

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:

Go
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)
}
SyntaxWhere it worksTypeWhen to prefer it
var x T = vPackages and functionsExplicit TPublic APIs, non-default types (int64, float32)
var x TPackages and functionsExplicit TInitialization to zero value (0, "", false)
x := vOnly inside functionsInferred from v90% 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):

Go
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:

Go
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 _:

Go
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

Exercise#go.m1.l1.e1
Attempts: 0Loading…

Declare with var a variable name of type string with value "Ada" inside main and print it.

Loading editor…
Show hint

The long form combines the var keyword, name, type, and assignment: var name string = "Ada".

Solution available after 3 attempts

Exercise#go.m1.l1.e2
Attempts: 0Loading…

In a single line inside main, use := to assign 36 to age and "Roma" to city, then print both.

Loading editor…
Show hint

The multiple short declaration separates names and values with commas: age, city := 36, "Roma".

Solution available after 3 attempts

Exercise#go.m1.l1.e4
Attempts: 0Loading…

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.

Loading editor…
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

Quiz#go.m1.l1.e3
Ready

Which of these declarations produces a compilation error if placed OUTSIDE a function (package level)?

Go
// (a)
var name string = "Ada"

// (b)
age := 36

// (c)
const PI = 3.14
Answer options
Quiz#go.m1.l1.e5
Ready

What does this Go program print about the final value of x?

Go
package main

import "fmt"

func main() {
	x := 10
	if true {
		x := 99
		_ = x
	}
	fmt.Println(x)
}
Answer options