[Golang] switch statement

2021-10-10 hit count image

Let;s see how to use the switch statement in Golang.

Outline

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

switch statement

The if statement checks a condition and executes a block of code if the condition is true, but the switch statement checks a value and executes a block of code depending on the value.

switch TARGET_VARIABLE{
  case VALUE_1:
    CODE_BLOCK
  case VALUE_2:
    CODE_BLOCK
  default:
    CODE_BLOCK
}

To check how to use the switch statement in Golang, make the main.go file and modify it like the below.

package main

import "fmt"

func main() {
  a := 2

  switch a {
  case 1:
    fmt.Println("a == 1")
  case 2:
    fmt.Println("a == 2")
  case 3:
    fmt.Println("a == 3")
  default:
    fmt.Println("a is not 1, 2 or 3")
  }
}

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

# go run main.go
a == 2

break & fallthrough

In other languages, when you use the switch statement, you should use the break to break the case of the switch statement. However, In Golang, there is no break statement.

So, in other languages, if you omit the break statement, the next case block is also executed. But, in Golang, there is no break statement, so if you want to execute the next case block together, you should use the fallthrough statement.

package main

import "fmt"

func main() {
  a := 2

  switch a {
  case 1:
    fmt.Println("a == 1")
  case 2:
    fmt.Println("a == 2")
    fallthrough
  case 3:
    fmt.Println("a == 3")
  default:
    fmt.Println("a is not 1, 2 or 3")
  }
}

When you execute the code above, you can see that the next case block is executed together like the below.

# go run main.go
a == 2
a == 3

Check multiple values

In Golang, you can check multiple values at the same time like the below.

switch TARGET_VARIABLE {
  case VALUE_1, VALUE_2:
    ...
  case VALUE_3, VALUE_4:
    ...
  case VALUE_5:
    ...
  default:
    ...
}

To check how to use the multiple values in Golagn, modify the main.go file like the below.

package main

import "fmt"

func main() {
  a := 2

  switch a {
  case 1, 3, 5, 7, 9:
    fmt.Println("Odd")
  case 2, 4, 6, 8:
    fmt.Println("Even")
  default:
    fmt.Println("Please insert 0 < value < 10.")
  }
}

When you execute the code above, you can see the following result.

# go run main.go
Even

Initialization statement

You can use the initialization statement in the switch statement like the if statement.

switch INITIALIZATION_STATEMENT; TARGET_VARIABLE{
  case VALUE_1, VALUE_2:
    ...
  case VALUE_3, VALUE_4:
    ...
  case VALUE_5:
    ...
  default:
    ...
}

To see how to use the initialization statement in the switch statement, modify the main.go file like the below.

package main

import "fmt"

func main() {
  switch a := 2; a {
  case 1, 3, 5, 7, 9:
    fmt.Println("Odd")
  case 2, 4, 6, 8:
    fmt.Println("Even")
  default:
    fmt.Println("Please insert 0 < value < 10.")
  }
}

When you execute the code above, you can see the following result.

# go run main.go
Even

Completed

Done! we’ve seen how to use the switch statement in Golang. Unlike the other languages, in Golang, you don’t need to use the break statement and if you want to execute the next case block together, you should use the fallthrough statement. Also, you can use the initialization statement in the switch statement like the if statement.

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

Posts