If Else

Let's learn about Go's conditional statement

If

  • Use if to express conditional statements

    • ex)

      package main
      
      import "fmt"
      
      func canIDrink(age int) bool {
       if age < 18 {
        return false
       }
       return true
      }
      
      func main() {
       fmt.Println(canIDrink(19))
      }
      

Variable expression

  • You can create a variable at the moment you use if

    • You can create a variable right inside the if, and use that variable right after the semicolon(;)

    • ex)

    • By doing this, you can let other people reading the code know that "the variable was created only for use in if-else conditions!"

Last updated