diff --git a/examples/switch/switch.go b/examples/switch/switch.go index 2d2ec2e..2bfcd7b 100644 --- a/examples/switch/switch.go +++ b/examples/switch/switch.go @@ -60,4 +60,21 @@ func main() { whatAmI(true) whatAmI(1) whatAmI("hey") + + // Every case includes a break by default. + // You can use the keyword 'fallthrough' to override this + // behaviour. In this example, we print 'FooBar' if num + // is even and 'Bar' if num is odd. + evenOdd := func(num int) { + switch { + case num%2 == 0: + fmt.Printf("Foo") + fallthrough + case num%2 == 1: + fmt.Printf("Bar") + } + fmt.Printf("\n") + } + evenOdd(2) + evenOdd(5) } diff --git a/examples/switch/switch.hash b/examples/switch/switch.hash index 54d2e9f..db81ec6 100644 --- a/examples/switch/switch.hash +++ b/examples/switch/switch.hash @@ -1,2 +1,2 @@ -2486fc553301cdeac9a26f3d0b3aed4143d9f4f0 -ZcDzdx3nYQn +e501748f7d89d213a594dfcc4ce48e4067fd93ea +k7kFpwIJaL5 diff --git a/examples/switch/switch.sh b/examples/switch/switch.sh index 42e5206..e8dd4f1 100644 --- a/examples/switch/switch.sh +++ b/examples/switch/switch.sh @@ -5,3 +5,5 @@ It's after noon I'm a bool I'm an int Don't know type string +FooBar +Bar \ No newline at end of file diff --git a/public/switch b/public/switch index f0eff96..64d15cf 100644 --- a/public/switch +++ b/public/switch @@ -42,7 +42,7 @@ branches.
package main
t
will have the
type corresponding to its clause.
whatAmI := func(i interface{}) {
switch t := i.(type) {
@@ -162,6 +162,33 @@ type corresponding to its clause.
whatAmI(true)
whatAmI(1)
whatAmI("hey")
+
Every case includes a break by default. +You can use the keyword ‘fallthrough’ to override this +behaviour. In this example, we print ‘FooBar’ if num +is even and ‘Bar’ if num is odd.
+ + evenOdd := func(num int) {
+ switch {
+ case num%2 == 0:
+ fmt.Printf("Foo")
+ fallthrough
+ case num%2 == 1:
+ fmt.Printf("Bar")
+ }
+ fmt.Printf("\n")
+ }
+ evenOdd(2)
+ evenOdd(5)
}