Skip to content
This repository has been archived by the owner on Nov 22, 2018. It is now read-only.

Commit

Permalink
go-tour: add Type Inference and Zero Values slides
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adg committed Jul 30, 2014
1 parent 20d25be commit e1dd6be
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
7 changes: 4 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions content/basics.article
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions content/basics/type-inference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "fmt"

func main() {
v := 42 // change me!
fmt.Printf("v is of type %T\n", v)
}
11 changes: 11 additions & 0 deletions content/basics/zero.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit e1dd6be

Please sign in to comment.