{"id":105,"date":"2024-08-31T11:57:31","date_gmt":"2024-08-31T04:57:31","guid":{"rendered":"https:\/\/gotutorial.org\/?page_id=105"},"modified":"2024-09-26T07:54:19","modified_gmt":"2024-09-26T00:54:19","slug":"go-if-else","status":"publish","type":"page","link":"https:\/\/www.gotutorial.org\/go-tutorial\/go-if-else\/","title":{"rendered":"Go if else"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the Go <code>if else<\/code> statement to perform conditional execution of code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go if statement<\/h2>\n\n\n\n<p>In programming, you often need to execute a code block when a condition is true. In Go, you can use the <code>if<\/code> statement to do so:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">if<\/span> condition {\n   <span class=\"hljs-comment\">\/\/ code block to execute<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>condition<\/code> is evaluated to a boolean value, which can be either <code>true<\/code> or <code>false<\/code>.<\/li>\n\n\n\n<li>If the condition is <code>true<\/code>, Go will execute the code block within the opening and closing curly braces <code>{}<\/code>. Otherwise, it will pass the control to the next statement after the <code>if<\/code> statement.<\/li>\n<\/ul>\n\n\n\n<p class=\"note\">Unlike programming languages such as <a href=\"https:\/\/www.javascripttutorial.net\/javascript-if\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a> and <a href=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-if\/\">C#<\/a>, Go requires curly braces <code>{}<\/code> in the <code>if<\/code> statement.<\/p>\n\n\n\n<p>The following flowchart illustrates how the Go if statement works:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/gotutorial.org\/wp-content\/uploads\/2024\/09\/go-if.svg\" alt=\"go if\" class=\"wp-image-438\"\/><\/figure>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">package<\/span> main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    temperature := <span class=\"hljs-number\">26<\/span>\n    <span class=\"hljs-keyword\">if<\/span> temperature &gt; <span class=\"hljs-number\">20<\/span> {\n        fmt.Println(<span class=\"hljs-string\">\"It's warm.\"<\/span>)\n    } \n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\">It<span class=\"hljs-string\">'s warm.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>temperature<\/code> is 26 which is greater than 20, therefore, the condition <code>temperature &gt; 20<\/code> is true. So the program displays the message <code>It's warm<\/code> on the screen.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Go if Statement with Initialization<\/h3>\n\n\n\n<p>Go allows you to include a <a href=\"https:\/\/gotutorial.org\/go-tutorial\/go-variables\/\">short variable declaration<\/a> in the <code>if<\/code> statement like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">package<\/span> main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    <span class=\"hljs-keyword\">if<\/span> temperature := <span class=\"hljs-number\">26<\/span> ; temperature &gt; <span class=\"hljs-number\">20<\/span> {\n        fmt.Println(<span class=\"hljs-string\">\"It's warm.\"<\/span>)\n    } \n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we declare and initialize the <code>temperature<\/code> variable within the <code>if<\/code> statement. The <code>temperature<\/code> variable will be accessible only within that block.<\/p>\n\n\n\n<p>In practice, you&#8217;ll use this pattern to call a function and handle the error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">if<\/span> err := fn(); err != nil {\n    <span class=\"hljs-comment\">\/\/ handle error here<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this statement, we call the fn() function and assign the return value to the err variable. The condition checks if an error occurs (err != nil) and handles it accordingly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go if&#8230;else statement<\/h2>\n\n\n\n<p>To execute another code block when a condition is <code>false<\/code>, you can use the <code>else<\/code> branch:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">if<\/span> condition {\n   <span class=\"hljs-comment\">\/\/ if block <\/span>\n} <span class=\"hljs-keyword\">else<\/span> {\n   <span class=\"hljs-comment\">\/\/ else lock <\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/gotutorial.org\/wp-content\/uploads\/2024\/09\/go-if-else.svg\" alt=\"Go if else\" class=\"wp-image-437\"\/><\/figure>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">package<\/span> main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    temperature := <span class=\"hljs-number\">19<\/span>\n    <span class=\"hljs-keyword\">if<\/span> temperature &gt; <span class=\"hljs-number\">20<\/span> {\n        fmt.Println(<span class=\"hljs-string\">\"It's warm.\"<\/span>)\n    } <span class=\"hljs-keyword\">else<\/span> {\n        fmt.Println(<span class=\"hljs-string\">\"It's cold.\"<\/span>)\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\">It<span class=\"hljs-string\">'s cold.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the temperature is 19, so the condition <code>temperature &gt; 20<\/code> is false and executes the code in the <code>else<\/code> branch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go if&#8230;else&#8230;if statement<\/h2>\n\n\n\n<p>So far, you have learned how to evaluate a single condition and execute a code block in the <code>if<\/code> or <code>else<\/code> branch.<\/p>\n\n\n\n<p>To evaluate multiple conditions and execute different blocks, you can chain multiple <code>if else<\/code> statements like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">if<\/span> condition1 {\n   <span class=\"hljs-comment\">\/\/ ...<\/span>\n} <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> condition2 {\n   <span class=\"hljs-comment\">\/\/ ...<\/span>\n} <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> condition <span class=\"hljs-number\">3<\/span> {\n   <span class=\"hljs-comment\">\/\/...<\/span>\n} <span class=\"hljs-keyword\">else<\/span> {\n   <span class=\"hljs-comment\">\/\/ ..<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/gotutorial.org\/wp-content\/uploads\/2024\/09\/go-if-else-if.svg\" alt=\"Go if else if\" class=\"wp-image-436\"\/><\/figure>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\"><span class=\"hljs-keyword\">package<\/span> main\n\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">\"fmt\"<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">func<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    temperature := <span class=\"hljs-number\">9<\/span>\n    <span class=\"hljs-keyword\">if<\/span> temperature &gt; <span class=\"hljs-number\">24<\/span> {\n        fmt.Println(<span class=\"hljs-string\">\"It's hot.\"<\/span>)\n    } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> temperature &lt;= <span class=\"hljs-number\">24<\/span> &amp;&amp; temperature &gt; <span class=\"hljs-number\">18<\/span> {\n        fmt.Println(<span class=\"hljs-string\">\"It's cold.\"<\/span>)\n    } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> temperature &lt;= <span class=\"hljs-number\">18<\/span> &amp;&amp; temperature &gt; <span class=\"hljs-number\">10<\/span> {\n        fmt.Println(<span class=\"hljs-string\">\"It's chilly.\"<\/span>)\n    } <span class=\"hljs-keyword\">else<\/span> {\n        fmt.Println(<span class=\"hljs-string\">\"It's cold.\"<\/span>)\n    }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Go\" data-shcb-language-slug=\"go\"><span><code class=\"hljs language-go\">It<span class=\"hljs-string\">'s cold.<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Go<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">go<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This example evaluates the temperature based on multiple conditions and displays the corresponding weather conditions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>if<\/code> and <code>if else<\/code> statements to assess a condition and execute a code block when the condition is true.<\/li>\n\n\n\n<li>Use the <code>if else if<\/code> statement to evaluate multiple conditions.<\/li>\n\n\n\n<li>Go requires the curly braces <code>{}<\/code> for the code blocks following <code>if<\/code>, <code>if else<\/code>, <code>if else if<\/code> statements, even if the block contains a single statement.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t<div class=\"wth-thumbs\">\n\t\t<button\n\t\t\tdata-post=\"105\"\n\t\t\tdata-post-url=\"https:\/\/www.gotutorial.org\/go-tutorial\/go-if-else\/\"\n\t\t\tdata-post-title=\"Go if else\"\n\t\t\tdata-response=\"1\"\n\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t>\n\t\t\t<svg\n\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\tfill=\"none\"\n\t\t\t\tstroke=\"currentColor\"\n\t\t\t\tstroke-width=\"2\"\n\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t>\n\t\t\t\t<path\n\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t><\/path>\n\t\t\t<\/svg>\n\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t<\/button>\n\n\t\t<button\n\t\t\tdata-response=\"0\"\n\t\t\tdata-post=\"105\"\n\t\t\tdata-post-url=\"https:\/\/www.gotutorial.org\/go-tutorial\/go-if-else\/\"\n\t\t\tdata-post-title=\"Go if else\"\n\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t>\n\t\t\t<svg\n\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\tfill=\"none\"\n\t\t\t\tstroke=\"currentColor\"\n\t\t\t\tstroke-width=\"2\"\n\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t>\n\t\t\t\t<path\n\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t><\/path>\n\t\t\t<\/svg>\n\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t<\/button>\n\t<\/div>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the Go if else statement to perform conditional execution of code.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":10,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-105","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/comments?post=105"}],"version-history":[{"count":9,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/105\/revisions"}],"predecessor-version":[{"id":439,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/105\/revisions\/439"}],"up":[{"embeddable":true,"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/pages\/10"}],"wp:attachment":[{"href":"https:\/\/www.gotutorial.org\/wp-json\/wp\/v2\/media?parent=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}