diff --git a/examples/switch/switch.go b/examples/switch/switch.go index fe95e63..231558b 100644 --- a/examples/switch/switch.go +++ b/examples/switch/switch.go @@ -10,7 +10,7 @@ func main() { // Here's a basic `switch`. i := 2 - fmt.Print("write ", i, " as ") + fmt.Print("Write ", i, " as ") switch i { case 1: fmt.Println("one") @@ -25,9 +25,9 @@ func main() { // `default` case in this example as well. switch time.Now().Weekday() { case time.Saturday, time.Sunday: - fmt.Println("it's the weekend") + fmt.Println("It's the weekend") default: - fmt.Println("it's a weekday") + fmt.Println("It's a weekday") } // `switch` without an expression is an alternate way @@ -36,26 +36,26 @@ func main() { t := time.Now() switch { case t.Hour() < 12: - fmt.Println("it's before noon") + fmt.Println("It's before noon") default: - fmt.Println("it's after noon") + fmt.Println("It's after noon") } // A type `switch` compares types instead of values. You // can use this to discover the the type of an interface // value. In this example, the variable `t` will have the // type corresponding to its clause. - whatAmI := func(i interface{}) string { + whatAmI := func(i interface{}) { switch t := i.(type) { case bool: - return "I am a bool" + fmt.Println("I'm a bool") case int: - return "I am an int" + fmt.Println("I'm an int") default: - return fmt.Sprintf("Can't handle type %T", t) + fmt.Printf("Don't know type %T\n", t) } } - fmt.Println(whatAmI(1)) - fmt.Println(whatAmI(true)) - fmt.Println(whatAmI("hey")) + whatAmI(true) + whatAmI(1) + whatAmI("hey") } diff --git a/examples/switch/switch.hash b/examples/switch/switch.hash index e6d7380..1bde959 100644 --- a/examples/switch/switch.hash +++ b/examples/switch/switch.hash @@ -1,2 +1,2 @@ -1040d0721b871f78f221a0e9e4a61ea3b0e6de70 -8b5CajPcHn +d255a1fe931fe471b745aeb66830b10216617479 +kxkBPpY_ue diff --git a/examples/switch/switch.sh b/examples/switch/switch.sh index 0d67c0b..42e5206 100644 --- a/examples/switch/switch.sh +++ b/examples/switch/switch.sh @@ -1,7 +1,7 @@ $ go run switch.go -write 2 as two -it's the weekend -it's before noon -I am an int -I am a bool -Can't handle type string +Write 2 as two +It's a weekday +It's after noon +I'm a bool +I'm an int +Don't know type string diff --git a/public/switch b/public/switch index 2f0b5ed..84ac576 100644 --- a/public/switch +++ b/public/switch @@ -40,7 +40,7 @@ branches.
package main
i := 2
- fmt.Print("write ", i, " as ")
+ fmt.Print("Write ", i, " as ")
switch i {
case 1:
fmt.Println("one")
@@ -105,9 +105,9 @@ in the same case
statement. We use the optional
switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
- fmt.Println("it's the weekend")
+ fmt.Println("It's the weekend")
default:
- fmt.Println("it's a weekday")
+ fmt.Println("It's a weekday")
}
@@ -121,15 +121,43 @@ to express if/else logic. Here we also show how the
case
expressions can be non-constants.
t := time.Now()
switch {
case t.Hour() < 12:
- fmt.Println("it's before noon")
+ fmt.Println("It's before noon")
default:
- fmt.Println("it's after noon")
+ fmt.Println("It's after noon")
}
+
A type switch
compares types instead of values. You
+can use this to discover the the type of an interface
+value. In this example, the variable t
will have the
+type corresponding to its clause.
whatAmI := func(i interface{}) {
+ switch t := i.(type) {
+ case bool:
+ fmt.Println("I'm a bool")
+ case int:
+ fmt.Println("I'm an int")
+ default:
+ fmt.Printf("Don't know type %T\n", t)
+ }
+ }
+ whatAmI(true)
+ whatAmI(1)
+ whatAmI("hey")
}
$ go run switch.go
-write 2 as two
-it's the weekend
-it's before noon
+Write 2 as two
+It's a weekday
+It's after noon
+I'm a bool
+I'm an int
+Don't know type string