{"id":9832,"date":"2022-08-19T11:28:19","date_gmt":"2022-08-19T11:28:19","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=9832"},"modified":"2024-04-15T09:46:19","modified_gmt":"2024-04-15T09:46:19","slug":"go-conditions-combining-conditional-statements-in-golang","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/","title":{"rendered":"Go Conditions"},"content":{"rendered":"<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Combining Conditional Statements in Golang&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:513,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0}\">Combining Conditional Statements in Golang<\/span><\/h2>\n<p>&nbsp;<\/p>\n<h4>Go supports the <a href=\"https:\/\/prwatech.in\/blog\/go-lang\/installation-of-go-windows\/\">usual<\/a> <a href=\"https:\/\/go.dev\/\">comparison<\/a> operators :<\/h4>\n<ol>\n<li>Less than &lt;<\/li>\n<li>Less than or equal &lt;=<\/li>\n<li>Gretter then &gt;<\/li>\n<li>Gretter than or equal &gt;=<\/li>\n<li>Equal to ==<\/li>\n<li>Not Equal to !=<\/li>\n<li>Logical AND &amp;&amp;<\/li>\n<li>Logical OR | |<\/li>\n<li>Logical NOT !<\/li>\n<\/ol>\n<ul>\n<li><strong><u>If Statement<\/u><\/strong><\/li>\n<\/ul>\n<p>If the statement is used to specify a block of Go Code to be executed if a condition is True.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 If\u00a0 condition {<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ conditions <\/strong><\/p>\n<p>}<\/p>\n<p>In the example below, we test two values to find out if 200 is greater than 180. If the condition is true, print some text<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\n\r\nimport \"fmt\"\r\n    \r\nfunc main() {\r\n    x := 200\r\n    y := 180\r\n    if x &gt; y {\r\n        fmt.Println(\"x is greater than y\")\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\\conditions&gt; go run if.go\r\nx is greater than y\r\n<\/pre>\n<ul>\n<li><strong><u>The else Statement<\/u><\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">Syntax:\r\n    If condition {\r\n        \/\/ executed if the condition is false\r\n    }else {\r\n        \/\/ executed if the condition is false\r\n    }\r\n<\/pre>\n<ul>\n<li><strong><u>Using The if-else Statement<\/u><\/strong><\/li>\n<\/ul>\n<ol>\n<li>In this example, time (20) is greater than 18, so the if condition is false. Because of this, we move on to the else condition and print to the screen &#8220;Good evening&#8221;. If the time was less than 18, the program would print &#8220;Good day&#8221;:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n  time := 20\r\n  if (time &lt; 18) {\r\n    fmt.Println(\"Good day.\")\r\n  } else {\r\n    fmt.Println(\"Good evening.\")\r\n  }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\conditions&gt; go run ifelse.go\r\nGood evening.\r\n<\/pre>\n<\/li>\n<li>In this example, the temperature is 14 so the condition for if is false so the code line inside the else statement is executed:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport (\"fmt\")\r\n\r\nfunc main() {\r\n  temperature := 14\r\n  if (temperature &gt; 15) {\r\n    fmt.Println(\"It is warm out there\")\r\n  } else {\r\n    fmt.Println(\"It is cold out there\")\r\n  }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\conditions&gt; go run ifelse2.go\r\nIt is cold out there\r\n<\/pre>\n<\/li>\n<li>Having the else brackets in a different line will raise an error:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n  temperature := 14\r\n  if (temperature &gt; 15) {\r\n    fmt.Println(\"It is warm out there.\")\r\n  } \t\t\t\/\/ this raises an error\r\n  else {\r\n    fmt.Println(\"It is cold out there.\")\r\n  }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\conditions&gt; go run ifelse3.go\r\n# command-line-arguments\r\n.\\ifelse3.go:10:3: syntax error: unexpected else, expecting }\r\n<\/pre>\n<ul>\n<li><strong>else if Statement<\/strong><\/li>\n<\/ul>\n<p>else if statement is used to specify a new condition if the first condition is false.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">Syntax:\r\nif condition1 {\r\n   \/\/ executed if condition1 is true\r\n} else if condition2 {\r\n   \/\/ executed if condition1 is false and condition2 is true\r\n} else {\r\n   \/\/ executed if condition1 and condition2 are both false\r\n}\r\n<\/pre>\n<\/li>\n<li>This program shows how to use an else if\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n  time := 22\r\n  if time &lt; 10 {\r\n    fmt.Println(\"Good morning.\")\r\n  } else if time &lt; 20 {\r\n    fmt.Println(\"Good day.\")\r\n  } else {\r\n    fmt.Println(\"Good evening.\")\r\n  }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">PS C:\\GO_Language\\conditions&gt; go run ifils4.go\r\nGood evening.\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li>Another example for the use of else if.\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n  a := 14\r\n  b := 14\r\n  if a &lt; b {\r\n\r\n    fmt.Println(\"a is less than b.\")\r\n  } else if a &gt; b {\r\n    fmt.Println(\"a is more than b.\")\r\ngo run\r\n  } else {\r\n    fmt.Println(\"a and b are equal.\")\r\n  }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\conditions&gt; go run ifelse5.go\r\na and b are equal.\r\n<\/pre>\n<p>&nbsp;<\/li>\n<li>If conditions1 and condition2 are both correct, only the code for condition1 is executed:\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">package main\r\nimport \"fmt\"\r\n\r\nfunc main() {\r\n  x := 30\r\n  if x &gt;= 10 {\r\n    fmt.Println(\"x is larger than or equal to 10.\")\r\n  } else if x &gt; 20\r\n    fmt.Println(\"x is larger than 20.\")\r\n  } else {\r\n    fmt.Println(\"x is less than 10.\")\r\n  }\r\n}\r\n<\/pre>\n<p>Output :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">PS C:\\GO_Language\\conditions&gt; go run ifelse6.go\r\nx is larger than or equal to 10.\r\n<\/pre>\n<p>Combining Conditional Statements in Golang<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Combining Conditional Statements in Golang &nbsp; Go supports the usual comparison operators : Less than &lt; Less than or equal &lt;= Gretter then &gt; Gretter than or equal &gt;= Equal to == Not Equal to != Logical AND &amp;&amp; Logical OR | | Logical NOT ! If Statement If the statement is used to specify [&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":[2024,1562,2026,1563,1561,2025],"class_list":["post-9832","post","type-post","status-publish","format-standard","hentry","category-go-lang","category-golang-modules","tag-combining-conditional-statements-in-golang-golang-control-flow-statements-if","tag-condition","tag-go-if-else-statement","tag-if-else-statement","tag-ifelse","tag-switch-and-for"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Combining Conditional Statements in Golang - Prwatech<\/title>\n<meta name=\"description\" content=\"Master Combining Conditional Statements in Golang - 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=\"Combining Conditional Statements in Golang - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Combining Conditional Statements in Golang - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/\" \/>\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-08-19T11:28:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-15T09:46:19+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/\",\"url\":\"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/\",\"name\":\"Combining Conditional Statements in Golang - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2022-08-19T11:28:19+00:00\",\"dateModified\":\"2024-04-15T09:46:19+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Combining Conditional Statements in Golang - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Conditions\"}]},{\"@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":"Combining Conditional Statements in Golang - Prwatech","description":"Master Combining Conditional Statements in Golang - 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":"Combining Conditional Statements in Golang - Prwatech","og_description":"Master Combining Conditional Statements in Golang - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2022-08-19T11:28:19+00:00","article_modified_time":"2024-04-15T09:46:19+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/","url":"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/","name":"Combining Conditional Statements in Golang - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2022-08-19T11:28:19+00:00","dateModified":"2024-04-15T09:46:19+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Combining Conditional Statements in Golang - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/go-lang\/go-conditions-combining-conditional-statements-in-golang\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Conditions"}]},{"@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\/9832","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=9832"}],"version-history":[{"count":4,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9832\/revisions"}],"predecessor-version":[{"id":11524,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/9832\/revisions\/11524"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=9832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=9832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=9832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}