And Or in Go

Find this useful? Support us: Star on GitHub 6
Category: Conditional | Language: Go

In Go, the "and" and "or" operators can be used to evaluate boolean expressions. The "and" operator is represented by && and the "or" operator is represented by ||.

The "and" operator evaluates to true if both of its operands evaluate to true, and false otherwise. For example:

a := true
b := false
c := true

if a && c {
fmt.Println("a and c are both true")
}

if b && c {
fmt.Println("b and c are both true")
} else {
fmt.Println("b and c are not both true")
}

In this example, the first 'if' statement will print "a and c are both true" because both 'a' and 'c' evaluate to true. The second 'if' statement will print "b and c are not both true" because 'b' is false.

The "or" operator evaluates to true if at least one of its operands evaluates to true, and false otherwise. For example:

a := true
b := false
c := true

if a || c {
fmt.Println("at least one of a and c is true")
}

if b || c {
fmt.Println("at least one of b and c is true")
} else {
fmt.Println("neither b nor c is true")
}

In this example, the first 'if' statement will print "at least one of a and c is true" because 'a' is true. The second 'if' statement will print "at least one of b and c is true" because 'c' is true.