From 3eb46d31315db1f43d99ee5bde78512e33f6172a Mon Sep 17 00:00:00 2001
From: subsr97
Date: Mon, 28 Oct 2019 20:25:17 +0530
Subject: [PATCH] Added example for fallthrough in switch
---
examples/switch/switch.go | 17 +++++++++++++++++
examples/switch/switch.hash | 4 ++--
examples/switch/switch.sh | 2 ++
public/switch | 35 ++++++++++++++++++++++++++++++++---
4 files changed, 53 insertions(+), 5 deletions(-)
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.
- 
+ 
@@ -147,7 +147,7 @@ value. In this example, the variable 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)
}
@@ -185,6 +212,8 @@ type corresponding to its clause.
I'm a bool
I'm an int
Don't know type string
+FooBar
+Bar
|
@@ -203,7 +232,7 @@ type corresponding to its clause.