This commit is contained in:
Mark McGranaghan 2012-09-23 12:59:46 -07:00
parent 618dec4fae
commit d7bb44ff42
14 changed files with 93 additions and 70 deletions

View File

@ -1,22 +0,0 @@
// A function can return multiple values.
package main
import "fmt"
func vals() (int, int) { // The `(int, int)` in this signature shows that the
return 3, 7 // function returns 2 ints.
}
func main() {
x, y := vals() // Use the 2 different return values from the call.
fmt.Println(x)
fmt.Println(y)
}
/*
$ go run 21-returns.go
3
7
*/

23
020-returns/returns.go Normal file
View File

@ -0,0 +1,23 @@
// ## Multiple Return Values
// In Go, a function can return multiple values.
// This feature is used often, for example to
// return a result and an error from a function.
package main
import "fmt"
// The `(int, int)` in this signature shows that the
// function returns 2 ints.
func vals() (int, int) {
return 3, 7
}
func main() {
// Use the 2 different return values from the call.
x, y := vals()
fmt.Println(x)
fmt.Println(y)
}

3
020-returns/returns.sh Normal file
View File

@ -0,0 +1,3 @@
$ go run returns.go
3
7

View File

@ -1,27 +0,0 @@
package main // Varadic functions can be called with any number of
// trailing arguments.
import "fmt"
func add(nums ...int) int { // Varadic args are declared with `...type` and
fmt.Print(nums, " ") // passed in as a slice.
total := 0
for _, num := range nums {
total += num
}
return total
}
func main() {
fmt.Println(add(1, 2)) // Varadic functions can be called in the usual way.
fmt.Println(add(1, 2, 3))
nums := []int{1, 2, 3, 4} // If you already have multiple args in a slice,
fmt.Println(add(nums...)) // apply them to a varadic function using `
} // func(slice...)`.
/*
$ go run varadic.go
[1 2] 3
[1 2 3] 6
[1 2 3 4] 10
*/

33
021-varadic/varadic.go Normal file
View File

@ -0,0 +1,33 @@
// ## Varadic Functions
// Varadic functions can be called with any number of
// trailing arguments. This is useful if you don't know
// number of arguments that will be needed for a function
// ahead of time.
package main
import "fmt"
// Varadic args are declared with `...type` and
// passed in as a slice.
func add(nums ...int) int {
fmt.Print(nums, " ")
total := 0
for _, num := range nums {
total += num
}
return total
}
func main() {
// Varadic functions can be called in the usual way.
fmt.Println(add(1, 2))
fmt.Println(add(1, 2, 3))
// If you already have multiple args in a slice,
// apply them to a varadic function using `
// func(slice...)`.
nums := []int{1, 2, 3, 4}
fmt.Println(add(nums...))
}

4
021-varadic/varadic.sh Normal file
View File

@ -0,0 +1,4 @@
$ go run varadic.go
[1 2] 3
[1 2 3] 6
[1 2 3 4] 10

View File

@ -1,3 +1,5 @@
// ## Closures
package main
import "fmt"

View File

@ -1,3 +1,5 @@
// ## Recursion
package main
import "fmt"

View File

@ -1,3 +1,5 @@
// ## Defer
package main
import "fmt"

View File

@ -1,21 +0,0 @@
// Panic
// A `panic` means something went unexpectedly wrong.
// Mostly we use it to fail fast on errors that
// shouldn't occur during normal operation.
package main
func main() {
panic("O noes") // We'll use panic throught this book to check for
} // unexpected errors. This is the only program in the
// book designed to panic.
/*
$ go run 26-panic.go
panic: O noes
goroutine 1 [running]:
main.main()
/.../src/26-panic.go:4 +0x47
...
*/

13
025-panic/panic.go Normal file
View File

@ -0,0 +1,13 @@
// ## Panic
// A `panic` means something went unexpectedly wrong.
// Mostly we use it to fail fast on errors that
// shouldn't occur during normal operation.
package main
func main() {
// We'll use panic throught this book to check for
// unexpected errors. This is the only program in the
// book designed to panic.
panic("O noes")
}

7
025-panic/panic.sh Normal file
View File

@ -0,0 +1,7 @@
$ go run 26-panic.go
panic: O noes
goroutine 1 [running]:
main.main()
/.../src/26-panic.go:4 +0x47
...

View File

@ -1,3 +1,5 @@
// ## Recover
package main
import "fmt"

View File

@ -1,3 +1,5 @@
// ## Pointers
package main
import "fmt"