Edits and generation for type switch update

This commit is contained in:
Mark McGranaghan
2016-12-27 12:41:21 -08:00
parent bee8f55277
commit a3b3bff8cb
4 changed files with 61 additions and 30 deletions

View File

@@ -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")
}

View File

@@ -1,2 +1,2 @@
1040d0721b871f78f221a0e9e4a61ea3b0e6de70
8b5CajPcHn
d255a1fe931fe471b745aeb66830b10216617479
kxkBPpY_ue

View File

@@ -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