From e1dd6bea2ce106a44bbd7b917f36c774ccc1c844 Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Wed, 30 Jul 2014 10:48:37 +1000 Subject: [PATCH] go-tour: add Type Inference and Zero Values slides Also check off a couple of finished things from the TODO list. LGTM=campoy R=campoy, golang-codereviews CC=golang-codereviews https://codereview.appspot.com/116090043 --- TODO | 7 ++++--- content/basics.article | 31 +++++++++++++++++++++++++++++++ content/basics/type-inference.go | 8 ++++++++ content/basics/zero.go | 11 +++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 content/basics/type-inference.go create mode 100644 content/basics/zero.go diff --git a/TODO b/TODO index 3a8ffe8..ec4fa3d 100644 --- a/TODO +++ b/TODO @@ -15,8 +15,9 @@ should be added (prefixed by -). It should be kept up-to-date with tour.article. * Variables with initializers * Short variable declarations * Basic types +* Type inference * Type conversions -- The zero value +* Zero values * Constants * Numeric Constants * For @@ -36,7 +37,7 @@ should be added (prefixed by -). It should be kept up-to-date with tour.article. * Slices * Slicing slices * Making slices -- Append +* Append - Copy * Nil slices * Range @@ -90,7 +91,7 @@ should be added (prefixed by -). It should be kept up-to-date with tour.article. * Exercise: Equivalent Binary Trees * Exercise: Web Crawler - More language features -- Defer +* Defer - Panic and recover - init functions - Tools diff --git a/content/basics.article b/content/basics.article index e620aa6..a3e345a 100644 --- a/content/basics.article +++ b/content/basics.article @@ -142,6 +142,18 @@ as with import statements. .play basics/basic-types.go +* Zero values + +Variables declared without an explicit initial value are given their +_zero_value_. + +The zero value is: +- `0` for numeric types, +- `false` the boolean type, and +- `""` (the empty string) for strings. + +.play basics/zero.go + * Type conversions The expression `T(v)` converts the value `v` to the type `T`. @@ -164,6 +176,25 @@ Try removing the `float64` or `int` conversions in the example and see what happ .play basics/type-conversions.go +* Type inference + +When declaring a variable without specifying its type (using `var` without a type or the `:=` syntax), the variable's type is _inferred_ from the value on the right hand side. + +When the right hand side of the declaration is typed, the new variable is of that same type: + + var i int + j := i // j is an int + +But when the right hand side contains an untyped numeric constant, the new variable may be an `int`, `float64`, or `complex128` depending on the precision of the constant: + + i := 42 // int + f := 3.142 // float64 + g := 0.867 + 0.5i // complex128 + +Try changing the initial value of `v` in the example code and observe how its type is affected. + +.play basics/type-inference.go + * Constants Constants are declared like variables, but with the `const` keyword. diff --git a/content/basics/type-inference.go b/content/basics/type-inference.go new file mode 100644 index 0000000..1db8ace --- /dev/null +++ b/content/basics/type-inference.go @@ -0,0 +1,8 @@ +package main + +import "fmt" + +func main() { + v := 42 // change me! + fmt.Printf("v is of type %T\n", v) +} diff --git a/content/basics/zero.go b/content/basics/zero.go new file mode 100644 index 0000000..9a391c9 --- /dev/null +++ b/content/basics/zero.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + var i int + var f float64 + var b bool + var s string + fmt.Printf("%v %v %v %q\n", i, f, b, s) +}