November 2017
Intermediate to advanced
670 pages
17h 35m
English
Type inference is the process of determining the appropriate types for expressions based on how they are used.
Go can determine that the type of the variable a in the following examples is int:
var a = 5a := 5
Go properly infers data types in many scenarios, such as the one here:
a := 1.8b := math.Floor(a + 1)fmt.Println("b:", reflect.TypeOf(b))
The following is the output:
b: float64
However, since Go does not fully implement the Hindley-Milner type system, Go fails to infer the type of b in this example:
a := 1b := math.Floor(a + 1.8)println(b)
Rather than inferring that the type of b is float64, Go reports the following compile errors:
constant 1.8 truncated to integercannot use a + 1.8 (type int) as type float64 in argument ...