357946ac18641191d9c67a3f0de580feeeb33f4e — kotrunga 8 months ago 9e6d25b
completed section 1.2
A basics/flow-control-statements/1.2.10_switch-evaluation-order.go => basics/flow-control-statements/1.2.10_switch-evaluation-order.go +28 -0
@@ 0,0 1,28 @@
+ /*
+ 	Notes 1.2.10
+ 
+ 	- Go's switch statements evaluate cases from top to bottom
+ 	- Mentioned already in 1.2.9, but the evaluation STOPS when a case succeeds
+ */
+ 
+ package main
+ 
+ import (
+ 	"fmt"
+ 	"time"
+ )
+ 
+ func main() {
+ 	fmt.Println("When's Saturday?")
+ 	today := time.Now().Weekday()
+ 	switch time.Saturday {
+ 	case today + 0:
+ 		fmt.Println("Today.")
+ 	case today + 1:
+ 		fmt.Println("Tomorrow.")
+ 	case today + 2:
+ 		fmt.Println("In two days.")
+ 	default:
+ 		fmt.Println("Too far away.")
+ 	}
+ }< 
\ No newline at end of file

A basics/flow-control-statements/1.2.11_switch-with-no-condition.go => basics/flow-control-statements/1.2.11_switch-with-no-condition.go +24 -0
@@ 0,0 1,24 @@
+ /*
+ 	Notes 1.2.11
+ 
+ 	- Go's switch statement without a condition is the same as: switch true
+ */
+ 
+ package main
+ 
+ import (
+ 	"fmt"
+ 	"time"
+ )
+ 
+ func main() {
+ 	t := time.Now()
+ 	switch {
+ 	case t.Hour() < 12:
+ 		fmt.Println("Good morning!")
+ 	case t.Hour() < 17:
+ 		fmt.Println("Good afternoon.")
+ 	default:
+ 		fmt.Println("Good evening.")
+ 	}
+ }< 
\ No newline at end of file

A basics/flow-control-statements/1.2.12_defer.go => basics/flow-control-statements/1.2.12_defer.go +16 -0
@@ 0,0 1,16 @@
+ /*
+ 	Notes 1.2.12
+ 
+ 	- A defer statement 'defers' (delays) the execution of a function
+ 	  until the surrounding function returns.
+ */
+ 
+ package main
+ 
+ import "fmt"
+ 
+ func main() {
+ 	defer fmt.Println("world!")
+ 
+ 	fmt.Println("Hello ")
+ }< 
\ No newline at end of file

A basics/flow-control-statements/1.2.13_defer-multi.go => basics/flow-control-statements/1.2.13_defer-multi.go +21 -0
@@ 0,0 1,21 @@
+ /*
+ 	Notes 1.2.13
+ 
+ 	- Deferred function calls are pushed onto a stack
+ 	- When the surrounding function returns, the deferred function calls
+ 	  are executed in last-in-first-out order. (B/c it's a stack)
+ */
+ 
+ package main
+ 
+ import "fmt"
+ 
+ func main() {
+ 	fmt.Println("counting")
+ 
+ 	for i := 0; i < 10; i++ {
+ 		defer fmt.Println(i)
+ 	}
+ 
+ 	fmt.Println("done")
+ }< 
\ No newline at end of file

A basics/flow-control-statements/1.2.9_switch.go => basics/flow-control-statements/1.2.9_switch.go +27 -0
@@ 0,0 1,27 @@
+ /*
+ 	Notes 1.2.9
+ 
+ 	- Go switch statements 'automatically have break statements'
+     (when a case matches, it's the only one that will be run)
+ */
+ 
+ package main
+ 
+ import (
+ 	"fmt"
+ 	"runtime"
+ )
+ 
+ func main() {
+ 	fmt.Print("Go runs on ")
+ 	switch os := runtime.GOOS; os {
+ 	case "darwin":
+ 		fmt.Println("OS X.")
+ 	case "linux":
+ 		fmt.Println("Linux.")
+ 	default:
+ 		// freebsd, openbsd,
+ 		// plan9, windows...
+ 		fmt.Println("%s.", os)
+ 	}
+ }< 
\ No newline at end of file