Go if Statement

Gayathri. B

The if Statement in Go

  • The if statement is used to run a block of code only when a specified condition is true.
Syntax:
if condition {
  // code to execute if condition is true
}
Note: The keyword if must be written in lowercase. Writing it as If or IF will cause a syntax error.

Example:

  • In this example, we check whether 20 is greater than 18. Since the condition is true, the message is printed:
package main
import "fmt"
func main() {
  if 20 > 18 {
    fmt.Println("20 is greater than 18")
  }
}

Output:

20 is greater than 18

Example2:

package main
import "fmt"
func main() {
    num1 := 20
    num2 := 18
    if num1 > num2 {
        fmt.Println("num1 is greater than num2")
    }
}
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send