Skip to main content

Command Palette

Search for a command to run...

Conditionals in Go

Updated
3 min read

Conditional constructs are very useful in programming as they allow complex logic to be implemented within code. In this blog post, I will go over the methods on how to execute a code block conditionally.

The first method is to use an if statement. The code block below demonstrates how this can be done:

import "fmt"

var a int = 1
if a == 1 {  // the condition checks whether a is equal to 1
      a++
}
fmt.Println(a)  
//output: 2

Note that the condition which is being evaluated does not have to be in brackets.

If/else statements can be used when we want code block 1 to be executed when condition a is true and code block 2 to be executed when condition a is false.

import "fmt"

var a int = 2
if a == 1 {
    fmt.Println("a is equal to 1")
} else if a < 1 {  // the condition checks whether a is less than 1
    fmt.Println("a is less than 1")
} else {  // proceed if both conditions above are false
    fmt.Println("a is greater than 1")
}
// output: a is greater than 1

Another conditional construct is a switch statement. A switch statement can be used when an expression needs to be compared to multiple different values. The code block below demonstrates how switch statements can be used:

import "fmt"

var a = 2
switch a {
    case 1: // does a == 1?
        fmt.Println("a is equal to 1")
    case 2: // does a == 2?
        fmt.Println("a is equal to 2")
}
// output: a is equal to 2

A switch statement can have a default case as well. An example below shows this:

import "fmt"

var a = 3
switch a {
    case 1:  // does a == 1?
        fmt.Println("a is equal to 1")
    case 2: // does a == 2?
        fmt.Println("a is equal to 2")
    default:
        fmt.Println("a is neither 1 nor 2")

// output: a is neither 1 nor 2

In both if/else and switch statements, it is possible to declare a variable inside the statement. Note that this variable would only exist within the scope of the conditional construct.

The code block below demonstrates how a variable can be declared in an if/else statement.

import "fmt"

if b := 10; b % 2 == 0 {  // define b = 10, check if b is divisible by 2
    b++  // increment 'b' by 1
    fmt.Println(b)
} else {
    b += 2  // increment 'b' by 2
    fmt.Println(b)
}
// output: 11

Note that it is not possible to print b outside of the if/else statement because it is only defined within the scope of the statement.

The code block below demonstrates how a variable can be declared inside a switch statement:

import "fmt"

switch b := 3; b {
    case 1:  // does b == 1?
        fmt.Println("b is equal to 1")
    case 2: // does b == 2?
        fmt.Println("b is equal to 2")
    default:
        fmt.Println("b is neither 1 nor 2")
// output: b is neither 1 nor 2

By tightly scoping a variable to where it is needed and by making the code more concise, declaring a variable within a conditional construct can improve code readability and maintainability.

More from this blog

GoTech With Alissa

19 posts