diff --git a/020-returns/020-returns.go b/020-returns/020-returns.go deleted file mode 100644 index a01a712..0000000 --- a/020-returns/020-returns.go +++ /dev/null @@ -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 -*/ diff --git a/020-returns/returns.go b/020-returns/returns.go new file mode 100644 index 0000000..1325a95 --- /dev/null +++ b/020-returns/returns.go @@ -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) +} diff --git a/020-returns/returns.sh b/020-returns/returns.sh new file mode 100644 index 0000000..0a344c6 --- /dev/null +++ b/020-returns/returns.sh @@ -0,0 +1,3 @@ +$ go run returns.go +3 +7 diff --git a/021-varadic/021-varadic.go b/021-varadic/021-varadic.go deleted file mode 100644 index d6f993e..0000000 --- a/021-varadic/021-varadic.go +++ /dev/null @@ -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 -*/ diff --git a/021-varadic/varadic.go b/021-varadic/varadic.go new file mode 100644 index 0000000..69fefb7 --- /dev/null +++ b/021-varadic/varadic.go @@ -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...)) +} diff --git a/021-varadic/varadic.sh b/021-varadic/varadic.sh new file mode 100644 index 0000000..300c303 --- /dev/null +++ b/021-varadic/varadic.sh @@ -0,0 +1,4 @@ +$ go run varadic.go +[1 2] 3 +[1 2 3] 6 +[1 2 3 4] 10 diff --git a/022-closures/022-closures.go b/022-closures/closures.go similarity index 94% rename from 022-closures/022-closures.go rename to 022-closures/closures.go index 7de5b73..f034e34 100644 --- a/022-closures/022-closures.go +++ b/022-closures/closures.go @@ -1,3 +1,5 @@ +// ## Closures + package main import "fmt" diff --git a/023-recursion/023-recursion.go b/023-recursion/recursion.go similarity index 91% rename from 023-recursion/023-recursion.go rename to 023-recursion/recursion.go index c9cb09c..f55c4d2 100644 --- a/023-recursion/023-recursion.go +++ b/023-recursion/recursion.go @@ -1,3 +1,5 @@ +// ## Recursion + package main import "fmt" diff --git a/024-defer/024-defer.go b/024-defer/defer.go similarity index 92% rename from 024-defer/024-defer.go rename to 024-defer/defer.go index d8fa3b8..6a39377 100644 --- a/024-defer/024-defer.go +++ b/024-defer/defer.go @@ -1,3 +1,5 @@ +// ## Defer + package main import "fmt" diff --git a/025-panic/025-panic.go b/025-panic/025-panic.go deleted file mode 100644 index 08510af..0000000 --- a/025-panic/025-panic.go +++ /dev/null @@ -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 -... -*/ diff --git a/025-panic/panic.go b/025-panic/panic.go new file mode 100644 index 0000000..6bf9e72 --- /dev/null +++ b/025-panic/panic.go @@ -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") +} diff --git a/025-panic/panic.sh b/025-panic/panic.sh new file mode 100644 index 0000000..83dc2ab --- /dev/null +++ b/025-panic/panic.sh @@ -0,0 +1,7 @@ +$ go run 26-panic.go +panic: O noes + +goroutine 1 [running]: +main.main() + /.../src/26-panic.go:4 +0x47 +... diff --git a/026-recover/026-recover.go b/026-recover/recover.go similarity index 90% rename from 026-recover/026-recover.go rename to 026-recover/recover.go index d5977be..32094af 100644 --- a/026-recover/026-recover.go +++ b/026-recover/recover.go @@ -1,3 +1,5 @@ +// ## Recover + package main import "fmt" diff --git a/027-pointers/027-pointers.go b/027-pointers/pointers.go similarity index 90% rename from 027-pointers/027-pointers.go rename to 027-pointers/pointers.go index 302ec78..cb6fcad 100644 --- a/027-pointers/027-pointers.go +++ b/027-pointers/pointers.go @@ -1,3 +1,5 @@ +// ## Pointers + package main import "fmt"