{"id":2088,"date":"2020-05-02T09:23:33","date_gmt":"2020-05-02T09:23:33","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=2088"},"modified":"2020-11-25T00:46:01","modified_gmt":"2020-11-24T19:16:01","slug":"understand-if-else-statement-golang","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/understand-if-else-statement-golang\/","title":{"rendered":"Understand If Else Statement in Go (Golang)"},"content":{"rendered":"\n

This is the\u00a0 chapter 12 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>\u00a0\u2013\u00a0Switch<\/a>
Previous Tutorial<\/strong>\u00a0\u2013
For Range loop<\/a><\/p>\n\n\n\n

Now let\u2019s check out the current tutorial. Below is the table of contents for current tutorial.<\/p>\n\n\n\n

Overview<\/strong><\/h1>\n\n\n\n

Go has if-else statement similar to any other programming language to perform the basic conditional logic. Below is the format for if-else statement in golang<\/p>\n\n\n\n

if condition {\n   \/\/Do something\n} else if condition {\n   \/\/Do something\n} else {\n   \/\/Do something<\/code><\/pre>\n\n\n\n

Before we move further let’s talk about the condition<\/strong> first. Only a statement or a combination of statements that result in a boolean are allowed for a condition in if. false boolean is treated as false in a condition in go and true boolean is treated as true. As mentioned above, the condition can be composed of multiple statements combined by operators in Go such as &&, ||, >, <, >=, <=, ! etc.<\/p>\n\n\n\n

Now let’s look into the if-else statement in detail to understand the small things. Go supports below formats for the if-else statement<\/p>\n\n\n\n