Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Enforce variables types

Each variable in Go has a type and Go will not let you mix values and variables of different types. When we created the variable with the := operator Go automatically deducted the type from the assigned value. In this case it was string.

package main

import (
	"fmt"
)

func main() {
	name := "Foo"
	fmt.Println(name)
	name = 42
}

# command-line-arguments
./variable.go:8:7: cannot use 42 (type int) as type string in assignment
  • Compile-time error