Web Development

Go Lang comparisons with the If/Else statement is straightforward.

If Statement

package main

import "fmt"

func main() {
  var cake string = "blueberry cheesecake"
  if cake == "blueberry cheesecake" {
    fmt.Println("Blueberry Cheesecake")
  }
}

Output

$ go run if.go
Blueberry Cheesecake

If/Else Statement

package main

import "fmt"

func main() {
  var cake string = "chocolate cake"
  if cake == "blueberry cheesecake" {
    fmt.Println("Blueberry Cheesecake")
  } else {
    fmt.Println("It is something else")
  }
}

Output

$ go run if.go
It is something else

Notes:

  • You do not need parenthesis enclosing conditions.