{"id":2097,"date":"2020-05-03T16:54:28","date_gmt":"2020-05-03T16:54:28","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=2097"},"modified":"2020-11-25T00:55:54","modified_gmt":"2020-11-24T19:25:54","slug":"switch-statement-golang","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/switch-statement-golang\/","title":{"rendered":"Switch Statement in Go (Golang)"},"content":{"rendered":"\n
This is the\u00a0 chapter 13 of the golang comprehensive tutorial series. Refer to this link for other chapters of the series \u2013\u00a0Golang Comprehensive Tutorial Series<\/a><\/p>\n\n\n\n Next Tutorial<\/strong> \u2013 Defer keyword<\/a> Switch statement are a perfect way to prevent a if-else ladder. Here is the format for switch statement<\/p>\n\n\n\n This is how switch works. Give a switch expression<\/strong>, it goes through all cases and tries to find the first case expression<\/strong> that matches the switch expression<\/strong> otherwise the default case is executed if present. The order of matching is from top to bottom and then left to right (when the case contains multiple expressions as we will see later in this tutorial).<\/p>\n\n\n\n Some important things to know about switch before we move to code examples<\/p>\n\n\n\n Both switch statement<\/strong> and switch expression<\/strong> are present<\/p>\n\n\n\n Only switch statement<\/strong> is present. Notice the syntax below. Semicolon needs to be present after switch statement<\/strong><\/p>\n\n\n\n Only switch expression<\/strong> is present. Notice the syntax below. No semicolon after switch expression<\/strong>.<\/p>\n\n\n\n Both switch statement<\/strong> and switch expression<\/strong> are absent.<\/p>\n\n\n\n Let’s see some simple example which illustrates the points above.<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n Some points to note:<\/p>\n\n\n\n Let’s see another example where we omit the switch statement<\/strong> as well as the switch expression<\/strong><\/p>\n\n\n\n Output<\/strong><\/p>\n\n\n\n Couple of points to notice about above example:<\/p>\n\n\n\n Notice the ';'<\/strong> after the statement<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n Output<\/strong><\/p>\n\n\n\n Two case statement cannot have the same constant. For example in below case there will be a compiler error raised as \"a\" is present in two case<\/p>\n\n\n\n See below code for fallthrough<\/strong> keyword example. In below example even though the second case matched it went through the third case because of fallthrough<\/strong> keyword<\/p>\n\n\n\n Output<\/strong><\/p>\n\n\n\n fallthrough<\/strong> needs to be final statement within the switch block. If it is not then compiler raise error<\/p>\n\n\n\n Below program will raise the above error as we have fmt.Println <\/strong>after the fallthrough<\/strong> statement<\/p>\n\n\n\n Below is the break<\/strong> statement example.<\/p>\n\n\n\n Output<\/strong><\/p>\n\n\n\n break<\/strong> statement will terminate the execution of the switch and below line below will never be executed<\/p>\n\n\n\n Switch statement can also be used to know the type of an interface at run time as shown in below example. The type switch compare types instead of values<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n This is all about switch statement in go. Hope you have liked the article. Please share feedback\/improvements\/mistakes in comments.<\/p>\n\n\n\n Next Tutorial<\/strong> \u2013 Defer keyword<\/a> This is the\u00a0 chapter 13 of the golang comprehensive tutorial series. Refer to this link for other chapters of the series \u2013\u00a0Golang Comprehensive Tutorial Series Next Tutorial \u2013 Defer keywordPrevious Tutorial \u2013 If Else Now…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4,291],"class_list":["post-2097","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang","tag-switch"],"yoast_head":"\n
Previous Tutorial<\/strong> \u2013 If Else<\/a>
Now let\u2019s check out the current tutorial. Below is the table of contents for current tutorial.<\/p>\n\n\n\nOverview<\/strong><\/h1>\n\n\n\n
switch statement; expression {\ncase expression1:\n \/\/Dosomething\ncase expression2:\n \/\/Dosomething\ndefault:\n \/\/Dosomething\n}<\/code><\/pre>\n\n\n\n
Important Points<\/strong><\/h1>\n\n\n\n
switch statement; expression {\n ... \n}<\/code><\/pre>\n\n\n\n
switch statement; {\n ...\n}<\/code><\/pre>\n\n\n\n
switch expression {\n ..\n}<\/code><\/pre>\n\n\n\n
switch {\n ... \n}<\/code><\/pre>\n\n\n\n
case expression1_1, expression_2 ...:<\/code><\/pre>\n\n\n\n
Examples<\/strong><\/h1>\n\n\n\n
Both switch statement and switch expression<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n switch ch := \"b\"; ch {\n case \"a\":\n fmt.Println(\"a\")\n case \"b\", \"c\":\n fmt.Println(\"b or c\") \n default:\n fmt.Println(\"No matching character\") \n }\n \n \/\/fmt.Println(ch)\n\n} <\/code><\/pre>\n\n\n\n
b or c<\/code><\/pre>\n\n\n\n
case \"b\", \"c\":<\/code><\/pre>\n\n\n\n
undefined: ch<\/code><\/pre>\n\n\n\n
Both switch statement and switch expression absent<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n age := 45\n switch {\n case age < 18:\n fmt.Println(\"Kid\")\n case age >= 18 && age < 40:\n fmt.Println(\"Young\")\n default:\n fmt.Println(\"Old\")\n }\n}<\/code><\/pre>\n\n\n\n
Old<\/code><\/pre>\n\n\n\n
Only switch statement<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n\n switch age := 29; {\n case age < 18:\n fmt.Println(\"Kid\")\n case age >= 18 && age < 40:\n fmt.Println(\"Young\")\n default:\n fmt.Println(\"Old\")\n }\n}<\/code><\/pre>\n\n\n\n
Young<\/code><\/pre>\n\n\n\n
Only switch expression<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n char := \"a\"\n switch char {\n case \"a\":\n fmt.Println(\"a\")\n case \"b\":\n fmt.Println(\"b\")\n default:\n fmt.Println(\"No matching character\")\n }\n}<\/code><\/pre>\n\n\n\n
a<\/code><\/pre>\n\n\n\n
Duplicate case<\/strong><\/h2>\n\n\n\n
duplicate case \"a\" in switch<\/code><\/pre>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n switch \"a\" {\n case \"a\":\n fmt.Println(\"a\")\n case \"a\":\n fmt.Println(\"Another a\")\n case \"b\":\n fmt.Println(\"b\")\n default:\n fmt.Println(\"No matching character\")\n }\n}<\/code><\/pre>\n\n\n\n
Fallthrough keyword<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n i := 45\n switch {\n case i < 10:\n fmt.Println(\"i is less than 10\")\n fallthrough\n case i < 50:\n fmt.Println(\"i is less than 50\")\n fallthrough\n case i < 100:\n fmt.Println(\"i is less than 100\")\n }\n}<\/code><\/pre>\n\n\n\n
i is less than 50\ni is less than 100<\/code><\/pre>\n\n\n\n
fallthrough statement out of place<\/code><\/pre>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n i := 45\n switch {\n case i < 10:\n fmt.Println(\"i is less than 10\")\n fallthrough\n case i < 50:\n fmt.Println(\"i is less than 50\")\n fallthrough\n fmt.Println(\"Not allowed\")\n case i < 100:\n fmt.Println(\"i is less than 100\")\n }\n}<\/code><\/pre>\n\n\n\n
Break statement<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n switch char := \"b\"; char {\n case \"a\":\n fmt.Println(\"a\")\n case \"b\":\n fmt.Println(\"b\")\n break\n fmt.Println(\"after b\")\n default:\n fmt.Println(\"No matching character\")\n }\n}<\/code><\/pre>\n\n\n\n
b<\/code><\/pre>\n\n\n\n
fmt.Println(\"after b\")<\/code><\/pre>\n\n\n\n
Type Switch<\/strong><\/h2>\n\n\n\n
package main\n\nimport \"fmt\"\n\nfunc main() {\n printType(\"test_string\")\n printType(2)\n}\n\nfunc printType(t interface{}) {\n switch v := t.(type) {\n case string:\n fmt.Println(\"Type: string\")\n case int:\n fmt.Println(\"Type: int\")\n default:\n fmt.Printf(\"Unknown Type %T\", v)\n }\n}<\/code><\/pre>\n\n\n\n
Type: string\nType: int<\/code><\/pre>\n\n\n\n
Conclusion<\/strong><\/h1>\n\n\n\n
Previous Tutorial<\/strong> \u2013 If Else<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"