OneBite.Dev - Coding blog in a bite size

if else conditional statement in Go

Code snippet on how to if else conditional statement in Go

  package main

import (
	"fmt"
)

func main() {
	x := 0

	if x > 0 {
		fmt.Println("x is greater than 0")
	} else if x < 0 {
		fmt.Println("x is less than 0")
	} else {
		fmt.Println("x is equal to 0")
	}
}

This code is an example of an if else conditional statement in Go. It starts with a main function, followed by the declaration of an integer x with a value of 0. After that, a standard if-else statement is used to evaluate the value of x. The first condition is “if x is greater than 0”, if that evaluates to true, then the program prints “x is greater than 0”, if it evaluates to false, the program proceeds to the second part of the if-else statement by testing if x is less than 0. If it evaluates to true, it prints “x is less than 0”, if false the program prints “x is equal to 0”.

go