Skip to Content
Learning Functional Programming in Go
book

Learning Functional Programming in Go

by Lex Sheehan
November 2017
Intermediate to advanced
670 pages
17h 35m
English
Packt Publishing
Content preview from Learning Functional Programming in Go

Type inference

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781787281394Supplemental Content