From 3712a36da98b6b938e3b9cdb3876a97232b643d1 Mon Sep 17 00:00:00 2001 From: asandikci Date: Mon, 10 Jul 2023 08:44:28 +0300 Subject: [PATCH] Switch --- 1flow/switch.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 1flow/switch.go diff --git a/1flow/switch.go b/1flow/switch.go new file mode 100644 index 0000000..7df8dca --- /dev/null +++ b/1flow/switch.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "runtime" + "time" +) + +func main() { + fmt.Println("Shorter way for lots of if else") + fmt.Println("No need to break after each statement") + fmt.Println("switch cases need not be constants, and the values involved need not be integers.") + + os := runtime.GOOS + + switch os { + case "windows": + fmt.Println("You are using windows") + case "linux": + fmt.Println("You are using linux") + default: + fmt.Printf("You are using %s\n", os) + } + + fmt.Println() + + fmt.Println("Also you can add short statement before condition too") + // like: switch os := runtime.GOOS; os { . . . } + + fmt.Println() + + fmt.Println("You can use switch with no condition, this is same as `switch true`") + t := time.Now() + fmt.Println(t) + switch { + case t.Hour() < 12: + fmt.Println("Good morning!") + case t.Hour() < 17: + fmt.Println("Good afternoon.") + default: + fmt.Println("Good evening.") + } +}