Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 1 | More types: structs, slices, and maps. |
| 2 | Learn how to define types based on existing ones: this lesson covers structs, arrays, slices, and maps. |
| 3 | |
| 4 | The Go Authors |
Andrew Gerrand | 148e778 | 2015-09-28 11:41:35 +1000 | [diff] [blame] | 5 | https://golang.org |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 6 | |
Andrew Gerrand | 7f9714c | 2014-07-17 13:27:17 +1000 | [diff] [blame] | 7 | * Pointers |
| 8 | |
| 9 | Go has pointers. |
Andrew Gerrand | c9941e5 | 2016-07-25 13:30:30 +1000 | [diff] [blame] | 10 | A pointer holds the memory address of a value. |
Andrew Gerrand | 7f9714c | 2014-07-17 13:27:17 +1000 | [diff] [blame] | 11 | |
| 12 | The type `*T` is a pointer to a `T` value. Its zero value is `nil`. |
| 13 | |
| 14 | var p *int |
| 15 | |
| 16 | The `&` operator generates a pointer to its operand. |
| 17 | |
| 18 | i := 42 |
| 19 | p = &i |
| 20 | |
| 21 | The `*` operator denotes the pointer's underlying value. |
| 22 | |
| 23 | fmt.Println(*p) // read i through the pointer p |
| 24 | *p = 21 // set i through the pointer p |
| 25 | |
| 26 | This is known as "dereferencing" or "indirecting". |
| 27 | |
| 28 | Unlike C, Go has no pointer arithmetic. |
| 29 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 30 | .play moretypes/pointers.go |
Andrew Gerrand | 7f9714c | 2014-07-17 13:27:17 +1000 | [diff] [blame] | 31 | |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 32 | * Structs |
| 33 | |
| 34 | A `struct` is a collection of fields. |
| 35 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 36 | .play moretypes/structs.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 37 | |
| 38 | * Struct Fields |
| 39 | |
| 40 | Struct fields are accessed using a dot. |
| 41 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 42 | .play moretypes/struct-fields.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 43 | |
Andrew Gerrand | 7f9714c | 2014-07-17 13:27:17 +1000 | [diff] [blame] | 44 | * Pointers to structs |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 45 | |
Andrew Gerrand | 7f9714c | 2014-07-17 13:27:17 +1000 | [diff] [blame] | 46 | Struct fields can be accessed through a struct pointer. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 47 | |
Emmanuel Odeke | a5983ab | 2016-02-28 21:50:40 -0700 | [diff] [blame] | 48 | To access the field `X` of a struct when we have the struct pointer `p` we could |
Andrew Gerrand | 1e3079b | 2016-02-26 15:23:49 +1100 | [diff] [blame] | 49 | write `(*p).X`. |
| 50 | However, that notation is cumbersome, so the language permits us instead to |
| 51 | write just `p.X`, without the explicit dereference. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 52 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 53 | .play moretypes/struct-pointers.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 54 | |
| 55 | * Struct Literals |
| 56 | |
| 57 | A struct literal denotes a newly allocated struct value by listing the values of its fields. |
| 58 | |
| 59 | You can list just a subset of fields by using the `Name:` syntax. (And the order of named fields is irrelevant.) |
| 60 | |
Andrew Gerrand | 7f9714c | 2014-07-17 13:27:17 +1000 | [diff] [blame] | 61 | The special prefix `&` returns a pointer to the struct value. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 62 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 63 | .play moretypes/struct-literals.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 64 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 65 | |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 66 | * Arrays |
| 67 | |
| 68 | The type `[n]T` is an array of `n` values of type `T`. |
| 69 | |
| 70 | The expression |
| 71 | |
| 72 | var a [10]int |
| 73 | |
| 74 | declares a variable `a` as an array of ten integers. |
| 75 | |
| 76 | An array's length is part of its type, so arrays cannot be resized. |
| 77 | This seems limiting, but don't worry; |
| 78 | Go provides a convenient way of working with arrays. |
| 79 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 80 | .play moretypes/array.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 81 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 82 | |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 83 | * Slices |
| 84 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 85 | An array has a fixed size. |
| 86 | A slice, on the other hand, is a dynamically-sized, |
| 87 | flexible view into the elements of an array. |
| 88 | In practice, slices are much more common than arrays. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 89 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 90 | The type `[]T` is a slice with elements of type `T`. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 91 | |
Rob Phoenix | d1a15fa | 2017-06-06 09:35:16 +0100 | [diff] [blame^] | 92 | A slice is formed by specifying two indices, a low and |
| 93 | high bound, separated by a colon: |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 94 | |
Rob Phoenix | d1a15fa | 2017-06-06 09:35:16 +0100 | [diff] [blame^] | 95 | a[low : high] |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 96 | |
Rob Phoenix | d1a15fa | 2017-06-06 09:35:16 +0100 | [diff] [blame^] | 97 | This selects a half-open range which includes the first |
| 98 | element, but excludes the last one. |
| 99 | |
| 100 | The following expression creates a slice which includes |
| 101 | elements 1 through 3 of `a`: |
| 102 | |
| 103 | a[1:4] |
Katrina Owen | 9d789a4 | 2015-10-07 20:52:49 -0600 | [diff] [blame] | 104 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 105 | .play moretypes/slices.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 106 | |
Katrina Owen | b9bdbaa | 2015-10-19 11:23:28 -0600 | [diff] [blame] | 107 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 108 | * Slices are like references to arrays |
Katrina Owen | b9bdbaa | 2015-10-19 11:23:28 -0600 | [diff] [blame] | 109 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 110 | A slice does not store any data, |
| 111 | it just describes a section of an underlying array. |
Katrina Owen | b9bdbaa | 2015-10-19 11:23:28 -0600 | [diff] [blame] | 112 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 113 | Changing the elements of a slice modifies the |
| 114 | corresponding elements of its underlying array. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 115 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 116 | Other slices that share the same underlying array will see those changes. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 117 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 118 | .play moretypes/slices-pointers.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 119 | |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 120 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 121 | * Slice literals |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 122 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 123 | A slice literal is like an array literal without the length. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 124 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 125 | This is an array literal: |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 126 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 127 | [3]bool{true, true, false} |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 128 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 129 | And this creates the same array as above, |
| 130 | then builds a slice that references it: |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 131 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 132 | []bool{true, true, false} |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 133 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 134 | .play moretypes/slice-literals.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 135 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 136 | |
| 137 | * Slice defaults |
| 138 | |
| 139 | When slicing, you may omit the high or low bounds to use their defaults instead. |
| 140 | |
| 141 | The default is zero for the low bound and the length of the slice for the high bound. |
| 142 | |
| 143 | For the array |
| 144 | |
| 145 | var a [10]int |
| 146 | |
| 147 | these slice expressions are equivalent: |
| 148 | |
| 149 | a[0:10] |
| 150 | a[:10] |
| 151 | a[0:] |
| 152 | a[:] |
| 153 | |
| 154 | .play moretypes/slice-bounds.go |
| 155 | |
| 156 | |
| 157 | * Slice length and capacity |
| 158 | |
| 159 | A slice has both a _length_ and a _capacity_. |
| 160 | |
| 161 | The length of a slice is the number of elements it contains. |
| 162 | |
| 163 | The capacity of a slice is the number of elements in the underlying array, |
| 164 | counting from the first element in the slice. |
| 165 | |
| 166 | The length and capacity of a slice `s` can be obtained using the expressions |
| 167 | `len(s)` and `cap(s)`. |
| 168 | |
| 169 | You can extend a slice's length by re-slicing it, |
| 170 | provided it has sufficient capacity. |
| 171 | Try changing one of the slice operations in the example program to extend it |
| 172 | beyond its capacity and see what happens. |
| 173 | |
| 174 | .play moretypes/slice-len-cap.go |
| 175 | |
| 176 | |
| 177 | * Nil slices |
| 178 | |
| 179 | The zero value of a slice is `nil`. |
| 180 | |
| 181 | A nil slice has a length and capacity of 0 |
| 182 | and has no underlying array. |
| 183 | |
| 184 | .play moretypes/nil-slices.go |
| 185 | |
| 186 | |
| 187 | * Creating a slice with make |
| 188 | |
| 189 | Slices can be created with the built-in `make` function; |
| 190 | this is how you create dynamically-sized arrays. |
| 191 | |
| 192 | The `make` function allocates a zeroed array |
| 193 | and returns a slice that refers to that array: |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 194 | |
| 195 | a := make([]int, 5) // len(a)=5 |
| 196 | |
| 197 | To specify a capacity, pass a third argument to `make`: |
| 198 | |
| 199 | b := make([]int, 0, 5) // len(b)=0, cap(b)=5 |
| 200 | |
| 201 | b = b[:cap(b)] // len(b)=5, cap(b)=5 |
| 202 | b = b[1:] // len(b)=4, cap(b)=4 |
| 203 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 204 | .play moretypes/making-slices.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 205 | |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 206 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 207 | * Slices of slices |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 208 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 209 | Slices can contain any type, including other slices. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 210 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 211 | .play moretypes/slices-of-slice.go |
Francesc Campoy | 57a43f2 | 2014-07-14 20:42:45 -0700 | [diff] [blame] | 212 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 213 | |
| 214 | * Appending to a slice |
Francesc Campoy | 57a43f2 | 2014-07-14 20:42:45 -0700 | [diff] [blame] | 215 | |
| 216 | It is common to append new elements to a slice, and so Go provides a built-in |
Andrew Gerrand | 148e778 | 2015-09-28 11:41:35 +1000 | [diff] [blame] | 217 | `append` function. The [[https://golang.org/pkg/builtin/#append][documentation]] |
Francesc Campoy | 57a43f2 | 2014-07-14 20:42:45 -0700 | [diff] [blame] | 218 | of the built-in package describes `append`. |
| 219 | |
| 220 | func append(s []T, vs ...T) []T |
| 221 | |
| 222 | The first parameter `s` of `append` is a slice of type `T`, and the rest are |
| 223 | `T` values to append to the slice. |
| 224 | |
| 225 | The resulting value of `append` is a slice containing all the elements of the |
| 226 | original slice plus the provided values. |
| 227 | |
| 228 | If the backing array of `s` is too small to fit all the given values a bigger |
| 229 | array will be allocated. The returned slice will point to the newly allocated |
| 230 | array. |
| 231 | |
Andrew Gerrand | 148e778 | 2015-09-28 11:41:35 +1000 | [diff] [blame] | 232 | (To learn more about slices, read the [[https://blog.golang.org/go-slices-usage-and-internals][Slices: usage and internals]] article.) |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 233 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 234 | .play moretypes/append.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 235 | |
Andrew Gerrand | 41d86d5 | 2016-04-01 17:12:52 +1100 | [diff] [blame] | 236 | |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 237 | * Range |
| 238 | |
| 239 | The `range` form of the `for` loop iterates over a slice or map. |
| 240 | |
Katrina Owen | 6cb86ee | 2015-10-08 11:53:05 -0600 | [diff] [blame] | 241 | When ranging over a slice, two values are returned for each iteration. |
| 242 | The first is the index, and the second is a copy of the element at that index. |
| 243 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 244 | .play moretypes/range.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 245 | |
| 246 | * Range continued |
| 247 | |
| 248 | You can skip the index or value by assigning to `_`. |
| 249 | |
| 250 | If you only want the index, drop the ", value" entirely. |
| 251 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 252 | .play moretypes/range-continued.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 253 | |
| 254 | * Exercise: Slices |
| 255 | |
| 256 | 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. |
| 257 | |
Andrew Gerrand | bb76bbf | 2015-06-17 08:23:23 +1000 | [diff] [blame] | 258 | The choice of image is up to you. Interesting functions include `(x+y)/2`, `x*y`, and `x^y`. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 259 | |
| 260 | (You need to use a loop to allocate each `[]uint8` inside the `[][]uint8`.) |
| 261 | |
| 262 | (Use `uint8(intValue)` to convert between types.) |
| 263 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 264 | .play moretypes/exercise-slices.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 265 | |
| 266 | * Maps |
| 267 | |
| 268 | A map maps keys to values. |
| 269 | |
Andrew Gerrand | 3a15bec | 2016-04-05 11:29:00 +1000 | [diff] [blame] | 270 | The zero value of a map is `nil`. |
Andrew Gerrand | 9b24cf3 | 2016-04-06 15:17:42 +1000 | [diff] [blame] | 271 | A `nil` map has no keys, nor can keys be added. |
Andrew Gerrand | 3010115 | 2016-04-04 11:46:10 +1000 | [diff] [blame] | 272 | |
Andrew Gerrand | 3a15bec | 2016-04-05 11:29:00 +1000 | [diff] [blame] | 273 | The `make` function returns a map of the given type, |
| 274 | initialized and ready for use. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 275 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 276 | .play moretypes/maps.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 277 | |
| 278 | * Map literals |
| 279 | |
| 280 | Map literals are like struct literals, but the keys are required. |
| 281 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 282 | .play moretypes/map-literals.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 283 | |
| 284 | * Map literals continued |
| 285 | |
| 286 | If the top-level type is just a type name, you can omit it from the elements of the literal. |
| 287 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 288 | .play moretypes/map-literals-continued.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 289 | |
| 290 | * Mutating Maps |
| 291 | |
| 292 | Insert or update an element in map `m`: |
| 293 | |
| 294 | m[key] = elem |
| 295 | |
| 296 | Retrieve an element: |
| 297 | |
| 298 | elem = m[key] |
| 299 | |
| 300 | Delete an element: |
| 301 | |
| 302 | delete(m, key) |
| 303 | |
| 304 | Test that a key is present with a two-value assignment: |
| 305 | |
| 306 | elem, ok = m[key] |
| 307 | |
Katrina Owen | 2582700 | 2015-10-07 23:59:40 -0600 | [diff] [blame] | 308 | If `key` is in `m`, `ok` is `true`. If not, `ok` is `false`. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 309 | |
Katrina Owen | 2582700 | 2015-10-07 23:59:40 -0600 | [diff] [blame] | 310 | If `key` is not in the map, then `elem` is the zero value for the map's element type. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 311 | |
Francesc Campoy | 4d627d9 | 2015-07-27 15:26:29 -0700 | [diff] [blame] | 312 | _Note_: if `elem` or `ok` have not yet been declared you could use a short declaration form: |
| 313 | |
| 314 | elem, ok := m[key] |
| 315 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 316 | .play moretypes/mutating-maps.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 317 | |
| 318 | * Exercise: Maps |
| 319 | |
| 320 | Implement `WordCount`. It should return a map of the counts of each “word” in the string `s`. The `wc.Test` function runs a test suite against the provided function and prints success or failure. |
| 321 | |
Andrew Gerrand | 148e778 | 2015-09-28 11:41:35 +1000 | [diff] [blame] | 322 | You might find [[https://golang.org/pkg/strings/#Fields][strings.Fields]] helpful. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 323 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 324 | .play moretypes/exercise-maps.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 325 | |
| 326 | * Function values |
| 327 | |
Katrina Owen | 500288f | 2015-10-11 11:39:41 -0600 | [diff] [blame] | 328 | Functions are values too. They can be passed around just like other values. |
| 329 | |
Katrina Owen | 64f07e5 | 2015-10-12 21:57:01 -0600 | [diff] [blame] | 330 | Function values may be used as function arguments and return values. |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 331 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 332 | .play moretypes/function-values.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 333 | |
| 334 | * Function closures |
| 335 | |
| 336 | Go functions may be closures. A closure is a function value that references variables from outside its body. The function may access and assign to the referenced variables; in this sense the function is "bound" to the variables. |
| 337 | |
| 338 | For example, the `adder` function returns a closure. Each closure is bound to its own `sum` variable. |
| 339 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 340 | .play moretypes/function-closures.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 341 | |
| 342 | * Exercise: Fibonacci closure |
| 343 | |
| 344 | Let's have some fun with functions. |
| 345 | |
Andrew Gerrand | e9f50ad | 2016-01-21 12:54:54 +1100 | [diff] [blame] | 346 | Implement a `fibonacci` function that returns a function (a closure) that |
| 347 | returns successive [[https://en.wikipedia.org/wiki/Fibonacci_number][fibonacci numbers]] |
| 348 | (0, 1, 1, 2, 3, 5, ...). |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 349 | |
Andrew Gerrand | c93c30e | 2014-07-23 13:47:53 +1000 | [diff] [blame] | 350 | .play moretypes/exercise-fibonacci-closure.go |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 351 | |
Francesc Campoy | 4d282f8 | 2014-02-25 18:28:40 -0800 | [diff] [blame] | 352 | * Congratulations! |
| 353 | |
| 354 | You finished this lesson! |
| 355 | |
Andrew Gerrand | 7f9714c | 2014-07-17 13:27:17 +1000 | [diff] [blame] | 356 | 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]]. |