{"id":9873,"date":"2022-09-03T05:30:00","date_gmt":"2022-09-03T05:30:00","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9873"},"modified":"2024-04-15T10:24:40","modified_gmt":"2024-04-15T10:24:40","slug":"switch-statement-in-golang-how-to-write-switch-statements-in-go","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/","title":{"rendered":"Switch Statement in Golang"},"content":{"rendered":"<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;How To Write Switch Statements in Go&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:513,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0}\">How To Write Switch Statements in Go<\/span><\/h2>\n<p>&nbsp;<\/p>\n<p>In Go, switch statements provide a concise and flexible way to evaluate expressions against multiple possible cases and execute corresponding blocks of code based on matching conditions. The syntax of a switch statement in Go is similar to other languages, but it offers several unique features.<\/p>\n<p>One notable feature of Go&#8217;s switch statement is that it does not require the use of explicit &#8220;break&#8221; statements after each case. Instead, Go automatically breaks out of the switch block after executing a matching case, preventing fall-through behavior by default.<\/p>\n<p>Go&#8217;s switch statements can evaluate expressions of various types, including integers, strings, and other types, allowing for versatile conditional logic. Additionally, Go allows multiple expressions in a single case, providing flexibility in matching conditions.<\/p>\n<p>Switch statements in Go can also include a default case, which is executed when none of the preceding cases match the expression&#8217;s value.<\/p>\n<h4>Go language has two <a href=\"https:\/\/prwatech.in\/blog\/go-lang\/installation-of-go-windows\/\">types<\/a> of <a href=\"https:\/\/go.dev\/\">switch<\/a> statements:<\/h4>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">switch expression {\r\ncase x:\r\n   \/\/ code block\r\ncase y:\r\n   \/\/ code block\r\ncase z:\r\n...\r\ndefault:\r\n   \/\/ code block\r\n}\r\n<\/pre>\n<ol>\n<li><strong>Expression Switch (Single case)<\/strong><\/li>\n<li><strong>Type Switch (Multi case)<\/strong><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ol>\n<li><strong><u>Single-case<\/u><\/strong><\/li>\n<\/ol>\n<p>1. The example below uses a number to return different text<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\nfunc main() {\r\n    fmt.Print(\"Enter the No. :\")\r\n    var I int\r\n    fmt.Scanln(&amp;I)\r\n    switch I {\r\n    case 1:\r\n        fmt.Print(\"The value is 10 \")\r\n    case 2:\r\n        fmt.Print(\"The value is 20 \")\r\n    case 3:\r\n        fmt.Print(\"The value is 30 \")\r\n    case 4:\r\n        fmt.Print(\"The value is 40 \")\r\n    case 5:\r\n    default:\r\n        fmt.Print(\"Incorrect input \")\r\n}\r\n}\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Case&gt; go run case1.go\r\nEnter No. :4\r\nThe value is 40 \r\nPS C:\\GO_Language\\Case&gt; go run case1.go\r\nEnter No. :2\r\nThe value is 20 \r\nPS C:\\GO_Language\\Case&gt; go run case1.go\r\nEnter No. :1\r\nThe value is 10 \r\nPS C:\\GO_Language\\Case&gt; go run case1.go\r\nEnter No. :6\r\nIt is not in bound\r\n<\/pre>\n<p>2.\u00a0 The example below uses a weekday number to calculate the weekday name:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport (\r\n    \"fmt\"\r\n)\r\n\r\nfunc main() {\r\n    fmt.Print(\"Enter Day No. :\")\r\n    var i int\r\n    fmt.Scanln(&amp;i)\r\n\r\n    switch i {\r\n    case 1:\r\n        fmt.Println(\"Monday\")\r\n    case 2:\r\n        fmt.Println(\"Tuesday\")\r\n    case 3:\r\n        fmt.Println(\"Wednesday\")\r\n    case 4:\r\n        fmt.Println(\"Thursday\")\r\n    case 5:\r\n        fmt.Println(\"Friday\")\r\n    case 6:\r\n        fmt.Println(\"Saturday\")\r\n    case 7:\r\n        fmt.Println(\"Sunday\")\r\n    default:\r\n        fmt.Print(\"It is not in bound\")\r\n\r\n    }\r\n} \r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Case&gt; go run case3.go\r\nEnter Day No. :1\r\nMonday\r\nPS C:\\GO_Language\\Case&gt; go run case3.go \r\nEnter Day No. :3\r\nWednesday\r\nPS C:\\GO_Language\\Case&gt; go run case3.go\r\nEnter Day No. :5\r\nFriday\r\nPS C:\\GO_Language\\Case&gt; go run case3.go\r\nEnter Day No. :6\r\nSaturday\r\n<\/pre>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong><u>2 . Multi-case<\/u><\/strong><\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>The example below uses number to return different text:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\nfunc main() {\r\n    var value string = \"five\"\r\n    switch value {\r\n    case \"one\":\r\n        fmt.Println(\"C#\")\r\n    case \"two\", \"three\":\r\n        fmt.Println(\"Go\")\r\n    case \"four\", \"five\", \"six\":\r\n        fmt.Println(\"java\")\r\n    }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\Case&gt; go run case2.go\r\njava\r\n<\/pre>\n<p>How To Write Switch Statements in Go<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>How To Write Switch Statements in Go &nbsp; In Go, switch statements provide a concise and flexible way to evaluate expressions against multiple possible cases and execute corresponding blocks of code based on matching conditions. The syntax of a switch statement in Go is similar to other languages, but it offers several unique features. One [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[666,1707],"tags":[1594,1593,1591,1592],"class_list":["post-9873","post","type-post","status-publish","format-standard","hentry","category-go-lang","category-golang-modules","tag-multi-case","tag-single-case","tag-switch-statement","tag-type-switch"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How To Write Switch Statements in Go - Prwatech<\/title>\n<meta name=\"description\" content=\"Master How to Write Switch Statements in Go - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How To Write Switch Statements in Go - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master How to Write Switch Statements in Go - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-03T05:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T10:24:40+00:00\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/\",\"url\":\"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/\",\"name\":\"How To Write Switch Statements in Go - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2022-09-03T05:30:00+00:00\",\"dateModified\":\"2024-04-15T10:24:40+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master How to Write Switch Statements in Go - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Switch Statement in Golang\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How To Write Switch Statements in Go - Prwatech","description":"Master How to Write Switch Statements in Go - Dive deep with our expert instructors and comprehensive curriculum.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"How To Write Switch Statements in Go - Prwatech","og_description":"Master How to Write Switch Statements in Go - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2022-09-03T05:30:00+00:00","article_modified_time":"2024-04-15T10:24:40+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/","url":"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/","name":"How To Write Switch Statements in Go - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2022-09-03T05:30:00+00:00","dateModified":"2024-04-15T10:24:40+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master How to Write Switch Statements in Go - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/go-lang\/switch-statement-in-golang-how-to-write-switch-statements-in-go\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Switch Statement in Golang"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=9873"}],"version-history":[{"count":4,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9873\/revisions"}],"predecessor-version":[{"id":11537,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9873\/revisions\/11537"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}