ZetCode

Go if & else statements

last modified August 24, 2023

Go if & else statements tutorial shows how to create conditions and branches in Golang.

Go if & else

The if statement specifies the conditional execution of a block. If the expression evaluates to true, the block is executed. If the else statement is present and the if statement evaluates to false, the block following else is executed.

There can be multiple if/else statements.

$ go version
go version go1.18.1 linux/amd64

We use Go version 1.18.

Go if/else examples

The following examples demonstrate conditional execution of blocks with if/else.

main.go
package main

import "fmt"

func main() {
    
    num := 4
    
    if num > 0 {

        fmt.Println("The number is positive")
    }
}

In the code example we have a simple condition; if the num variable is positive, the message "The number is positive" is printed to the console. Otherwise; nothing is printed.

$ go run if_stm.go 
The number is positive

The message is printed since value 4 is positive.


We add another branch with else.

main.go
package main

import "fmt"

func main() {
    
    num := -4
    
    if num > 0 {

        fmt.Println("The number is positive")
    } else {

        fmt.Println("The number is negative")
    }
}

The else statement specifies the block that is executed if the if condition fails.

$ go run if_else.go 
The number is negative

For the -4 value, the "The number is negative" is printed.


Next we add additional branch with if else.

main.go
package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    
    rand.Seed(time.Now().UnixNano())

    num := -5 + rand.Intn(10)

    if num > 0 {

        fmt.Println("The number is positive")
    } else if num == 0 {

        fmt.Println("The number is zero")
    } else {

        fmt.Println("The number is negative")
    }
}

We generate random values between -5 and 4. With the help of the if & else statement we print a message for all three options.

$ go run if_else2.go 
4
The number is positive
$ go run if_else.go 
0
The number is zero
$ go run if_else.go 
-5
The number is negative

We run the example a few times.


The if statement can start with a short statement to execute before the condition.

main.go
package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    
    rand.Seed(time.Now().UnixNano())

    if  num := -5 + rand.Intn(10); num > 0 {

        fmt.Println("value is positive")
    } else {

        fmt.Println("value is negative")
    }
}

The previous example is written with the short if statement.

Go check map key existence

Go has a shorthand notation for checking the existence of a key in a map.

main.go
package main

import "fmt"

func main() {

    grades := map[string]int{
        "Lucia": 2,
        "Paul":  1,
        "Merry": 3,
        "Jane":  1,
    }

    if g, found := grades["Jane"]; found {
        fmt.Println(g)
    }
}

We check if a grade for a particular student exists and if it does, we print its corresponding value.

In this article we have covered if/else conditions in Golang.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Go tutorials.