go-tour: add discussion of pointers and some small tweaks - Mention Sprint instead of Print when discussing recursive stringers. - Remove discussion of the "new" function; it's rarely used today. - Better hint in the cube root exercise (Who knows the cube root of 2?). - Use v to refer to Vertex and p to refer to a pointer. LGTM=campoy R=campoy CC=golang-codereviews https://golang.org/cl/117840043
diff --git a/content/methods.article b/content/methods.article index dd81829..bf9339f 100644 --- a/content/methods.article +++ b/content/methods.article
@@ -91,7 +91,7 @@ method such that `ErrNegativeSqrt(-2).Error()` returns `"cannot`Sqrt`negative`number:`-2"`. -*Note:* a call to `fmt.Print(e)` inside the `Error` method will send the program into an infinite loop. You can avoid this by converting `e` first: `fmt.Print(float64(e))`. Why? +*Note:* a call to `fmt.Sprint(e)` inside the `Error` method will send the program into an infinite loop. You can avoid this by converting `e` first: `fmt.Sprint(float64(e))`. Why? Change your `Sqrt` function to return an `ErrNegativeSqrt` value when given a negative number. @@ -184,4 +184,4 @@ You finished this lesson! -You can go back to the list of [[/list][modules]] to find what to learn next, or continue with the [[javascript:click('.next-page')][next lesson]]. \ No newline at end of file +You can go back to the list of [[/list][modules]] to find what to learn next, or continue with the [[javascript:click('.next-page')][next lesson]].
diff --git a/content/moretypes.article b/content/moretypes.article index 7d717f5..28e9298 100644 --- a/content/moretypes.article +++ b/content/moretypes.article
@@ -4,6 +4,31 @@ The Go Authors http://golang.org +* Pointers + +Go has pointers. +A pointer holds the memory address of a variable. + +The type `*T` is a pointer to a `T` value. Its zero value is `nil`. + + var p *int + +The `&` operator generates a pointer to its operand. + + i := 42 + p = &i + +The `*` operator denotes the pointer's underlying value. + + fmt.Println(*p) // read i through the pointer p + *p = 21 // set i through the pointer p + +This is known as "dereferencing" or "indirecting". + +Unlike C, Go has no pointer arithmetic. + +.play prog/tour/pointers.go + * Structs A `struct` is a collection of fields. @@ -18,13 +43,13 @@ .play prog/tour/struct-fields.go -* Pointers +* Pointers to structs -Go has pointers, but no pointer arithmetic. +Struct fields can be accessed through a struct pointer. -Struct fields can be accessed through a struct pointer. The indirection through the pointer is transparent. +The indirection through the pointer is transparent. -.play prog/tour/pointers.go +.play prog/tour/struct-pointers.go * Struct Literals @@ -32,22 +57,10 @@ You can list just a subset of fields by using the `Name:` syntax. (And the order of named fields is irrelevant.) -The special prefix `&` constructs a pointer to a newly allocated struct. +The special prefix `&` returns a pointer to the struct value. .play prog/tour/struct-literals.go -* The new function - -The expression `new(T)` allocates a zeroed `T` value and returns a pointer to it. - - var t *T = new(T) - -or - - t := new(T) - -.play prog/tour/the-new-function.go - * Arrays The type `[n]T` is an array of `n` values of type `T`. @@ -135,7 +148,7 @@ Implement `Pic`. It should return a slice of length `dy`, each element of which is a slice of `dx` 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values. -The choice of image is up to you. Interesting functions include `x^y`, `(x+y)/2`, and `x*y`. +The choice of image is up to you. Interesting functions include `(x+y)/2`, `x*y`, and `x^y` (to compute the latter function, use [[http://golang.org/pkg/math/#Pow][`math.Pow`]]). (You need to use a loop to allocate each `[]uint8` inside the `[][]uint8`.) @@ -224,7 +237,7 @@ .image /content/img/newton3.png -Find the cube root of 2, just to make sure the algorithm works. There is a [[http://golang.org/pkg/math/cmplx/#Pow][Pow]] function in the `math/cmplx` package. +Find the cube root of 8, just to make sure the algorithm works. (There is a [[http://golang.org/pkg/math/cmplx/#Pow][Pow]] function in the `math/cmplx` package to check your results.) .play prog/tour/advanced-exercise-complex-cube-roots.go @@ -232,4 +245,4 @@ You finished this lesson! -You can go back to the list of [[/list][modules]] to find what to learn next, or continue with the [[javascript:click('.next-page')][next lesson]]. \ No newline at end of file +You can go back to the list of [[/list][modules]] to find what to learn next, or continue with the [[javascript:click('.next-page')][next lesson]].
diff --git a/content/prog/tour/exercise-errors.go b/content/prog/tour/exercise-errors.go index b8554e4..22e933b 100644 --- a/content/prog/tour/exercise-errors.go +++ b/content/prog/tour/exercise-errors.go
@@ -6,7 +6,7 @@ "fmt" ) -func Sqrt(f float64) (float64, error) { +func Sqrt(x float64) (float64, error) { return 0, nil }
diff --git a/content/prog/tour/pointers.go b/content/prog/tour/pointers.go index daf0464..9909b3c 100644 --- a/content/prog/tour/pointers.go +++ b/content/prog/tour/pointers.go
@@ -4,14 +4,15 @@ import "fmt" -type Vertex struct { - X int - Y int -} - func main() { - p := Vertex{1, 2} - q := &p - q.X = 1e9 - fmt.Println(p) + i, j := 42, 2701 + + p := &i // point to i + fmt.Println(*p) // read i through the pointer + *p = 21 // set i through the pointer + fmt.Println(i) // see the new value of i + + p = &j // point to j + *p = *p / 37 // divide j through the pointer + fmt.Println(j) // see the new value of j }
diff --git a/content/prog/tour/struct-literals.go b/content/prog/tour/struct-literals.go index 20d3a90..5a96b2c 100644 --- a/content/prog/tour/struct-literals.go +++ b/content/prog/tour/struct-literals.go
@@ -9,12 +9,12 @@ } var ( - p = Vertex{1, 2} // has type Vertex - q = &Vertex{1, 2} // has type *Vertex - r = Vertex{X: 1} // Y:0 is implicit - s = Vertex{} // X:0 and Y:0 + v1 = Vertex{1, 2} // has type Vertex + v2 = Vertex{X: 1} // Y:0 is implicit + v3 = Vertex{} // X:0 and Y:0 + p = &Vertex{1, 2} // has type *Vertex ) func main() { - fmt.Println(p, q, r, s) + fmt.Println(v1, p, v2, v3) }
diff --git a/content/prog/tour/the-new-function.go b/content/prog/tour/struct-pointers.go similarity index 61% rename from content/prog/tour/the-new-function.go rename to content/prog/tour/struct-pointers.go index cc4db9f..901f003 100644 --- a/content/prog/tour/the-new-function.go +++ b/content/prog/tour/struct-pointers.go
@@ -5,12 +5,13 @@ import "fmt" type Vertex struct { - X, Y int + X int + Y int } func main() { - v := new(Vertex) - fmt.Println(v) - v.X, v.Y = 11, 9 + v := Vertex{1, 2} + p := &v + p.X = 1e9 fmt.Println(v) }