[x/tour] 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://golang.org/cl/116090043 X-Tour-Commit: f82b5da64d2d1a0de764ca341592c8ecf83079f1
This commit is contained in:
Родитель
437760d1cb
Коммит
c118623ff7
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
v := 42 // change me!
|
||||
fmt.Printf("v is of type %T\n", v)
|
||||
}
|
|
@ -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)
|
||||
}
|
Загрузка…
Ссылка в новой задаче