[Golang] if statement

2021-10-07 hit count image

Let's see how to use if statement in Golang.

Outline

I will introduce how to use the if statement in Golang. You can see the full source code of this blog post on the link below.

If statement

In Golang, you can use the if statement like the below. The if statement checks the condition and execute the code block if the condition is true.

if CONDITION {
  CODE_BLOCK
} else if CONDITION {
  CODE_BLOCK
} else {
  CODE_BLOCK
}

To check the if statement, create the main.go file and modify it like the below.

package main

import "fmt"

func main() {
  v := 89

  if v > 90 {
    fmt.Println("B")
  } else if v > 80 {
    fmt.Println("B")
  } else if v > 70 {
    fmt.Println("C")
  } else {
    fmt.Println("F")
  }
}

When you execute the program, you can see the result like the below.

# go run main.go
B

Short circuit

The if statement in Golang uses the short circuit.

false && CONDITION

When the Logical operator is executed, the first condition is executed, and then the second condition is executed. The && operator becomes true when all conditions are true. So, when the first condition is false like above, the logical operator can’t become true, so there is no need to execute the second condition.

true || CONDITION

The or operator becomes true when one of the conditions is true. So, when the first condition is already true like above, there is no need to execute the second condition.

To check the short circuit in Golang, modify the main.go file like the below.

package main

import "fmt"

func first(result bool) bool {
  fmt.Println("first condition is called!")
  return result
}

func second() bool {
  fmt.Println("Second condition is called!")
  return true
}

func main() {
  fmt.Println("ex 1")
  if first(false) && second() {
  }

  fmt.Println("ex 2")
  if first(true) && second() {
  }

  fmt.Println("ex 3")
  if first(true) || second() {
  }

  fmt.Println("ex 4")
  if first(false) || second() {
  }
}

When you execute the program, you can see the result like the below.

ex 1
first condition is called!
ex 2
first condition is called!
Second condition is called!
ex 3
first condition is called!
ex 4
first condition is called!
Second condition is called!

As the result above, you can see the short circuit works well in Golang.

Initialization statement

Unlike other languages, you can use the initialization statement in the if statement.

if INITIALIZATION_STATEMENT; CONDITION {
  CODE_BLOCK
}

The vairables in the initialization statement can be used in the condition or the code block.

func testFunc() (int, bool) {
  return 1, true
}

func main() {
  if v, success := testFunc(); success {
   fmt.Println(v)
  }

  fmt.Println(v) // ERROR
}

The variables in the initialization statement can’t be used outside the if statement, so when you use the variables like above, the compile error occurs.

Completed

Done! we’ve seen how to use the if statement in Golang. Also, we’ve seen the Golang unique feature that is the initialization statement in the if statement.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

Posts