Introduction
Type inference lets Go figure out the type of a variable automatically so you don't have to write it. This makes your code shorter and easier to read.
variableName := value
x := 10name := "Alice"pi := 3.14flag := truepackage main import "fmt" func main() { age := 25 city := "Paris" temperature := 18.5 isRaining := false fmt.Println("Age:", age) fmt.Println("City:", city) fmt.Println("Temperature:", temperature) fmt.Println("Is it raining?:", isRaining) }
x := 10 inside a function?x as int automatically.x is an int. -> Option B:= declares and assigns a variable with inferred type.package main
import "fmt"
func main() {
x := 3.14
fmt.Printf("%T", x)
}func main() {
var x := 10
fmt.Println(x)
}result after execution?
func main() {
a := 5
b := 2.0
result := a * int(b)
fmt.Printf("%T", result)
}Pass the test and this pattern moves unknown