# Switch

> Let's learn about Go's Switch

\ <br>

### Switch

: A way to check values

ex)

```go
package main

import "fmt"

func canIDrink(age int) bool {
 switch age {
 case 18:
  return false
 case 19:
  return true
 }
 return false
}

func main() {
 fmt.Println(canIDrink(19))
}
```

<br>

#### You can use switch to avoid rampant `if-else`, `else if` cases

ex)

```go
package main

import "fmt"

// Avoid if else with switch
func canIDrive(age int) bool {
 switch {
 case age < 19:
  return false
 case age == 19:
  return true
 case age > 19:
  return true
 }
 return false
}

func main() {
 fmt.Println(canIDrive(18))
}
```

<br>

#### You can also use `variable expression` in switch

ex)

```go
package main

import "fmt"

// Variable expression in switch
func canIVote(age int) bool {
 switch koreanAge := age + 2; {
 case koreanAge < 19:
  return false
 case koreanAge == 19:
  return true
 case koreanAge > 19:
  return true
 }
 return false
}

func main() {
 fmt.Println(canIVote(29))
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chloe-codes1.gitbook.io/til/go/go-101/06_switch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
