How to Combine Conditions with AND (&&), OR (||) and NOT (!) in Go
Go combines true-or-false conditions with three logical operators: a && b is true when both sides are true, a || b when at least one is, and !a turns true into false and false into true. && and || skip their right side when the left side already decides the answer, which lets a check such as len(roles) > 0 protect the code after it.
Syntax
<a> && <b>
<a> || <b>
!<a>
| Part | What it does |
|---|---|
<a> && <b> | AND: true only when both <a> and <b> are true. If <a> is false, <b> isn't evaluated. |
<a> || <b> | OR: true when <a>, <b> or both are true. If <a> is true, <b> isn't evaluated. |
!<a> | NOT: true when <a> is false, and false when it's true. |
<a>, <b> | Expressions of type bool, such as age >= 65 or a bool variable. |
The Go specification, Logical operators: "Logical operators apply to boolean values and yield a result of the same type as the operands. The left operand is evaluated, and then the right if the condition requires it."
The Example
The example checks a user who is signed in, isn't banned and is 70. It prints one &&, one || and one !, then uses && to check that a slice of roles isn't empty before it reads the first role. A changed version of the program then reads the first role without that check.
Prerequisites
- Go.
go runcompiles and runs a single file. This guide uses Go 1.27.4.
Step 1: Write the Code
Create a file named main.go:

package main
import "fmt"
func main() {
signedIn := true
banned := false
age := 70
// && is true only when both sides are true.
fmt.Println("can comment:", signedIn && !banned)
// || is true when at least one side is true.
fmt.Println("pays the reduced price:", age < 12 || age >= 65)
// ! turns true into false, and false into true.
fmt.Println("must sign in:", !signedIn)
var roles []string // no roles yet: an empty slice
// && skips its right side when the left side is false, so roles[0] is never read.
fmt.Println("first role is admin:", len(roles) > 0 && roles[0] == "admin")
}
var roles []string declares a slice with no elements, the way a user with no roles would arrive.
Step 2: Run It
go run main.go
can comment: true
pays the reduced price: true
must sign in: false
first role is admin: false
The command is the same in zsh, bash and PowerShell.
Step 3: Read the First Role Without the Check
Replace main.go with a version that compares the first role without checking the slice's length first:
package main
import "fmt"
func main() {
var roles []string // no roles yet: an empty slice
// Without the length check, roles[0] is read from an empty slice.
fmt.Println("first role is admin:", roles[0] == "admin")
}
go run main.go
panic: runtime error: index out of range [0] with length 0
goroutine 1 [running]:
main.main()
/Users/Shared/you/main.go:8 +0x1c
exit status 2
![A zsh terminal where go run main.go prints can comment: true, pays the reduced price: true, must sign in: false and first role is admin: false, then, for the changed file, stops with panic: runtime error: index out of range [0] with length 0 and exit status 2.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/go/2026/go-logical-zsh.webp)
What Happened
signedIn && !bannedistrue.!bannedturnedfalseintotrue, so both sides were true.age < 12 || age >= 65istrue. The left side was false, but the right side was true for an age of 70.!signedInisfalse, becausesignedInistrue.len(roles) > 0 && roles[0] == "admin"isfalse, with no error. The slice was empty, so the left side was false and&&never evaluatedroles[0].- Without the length check, the program stopped.
roles[0]asked for the first element of an empty slice. The specification's Index expressions say that for a slice, "if x is out of range at run time, a run-time panic occurs". The panic names the index and the length,main.go:8is the line that readroles[0], andgo runthen printedexit status 2.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using a number with &&, as in age && signedIn | invalid operation: age && signedIn (mismatched types int and bool), because Go never treats a number as true or false | Compare the number first, as in age >= 65 |
Writing !age >= 65 to mean "not 65 or over" | invalid operation: operator ! not defined on age (variable of type int), because ! applies to age first | Add parentheses: !(age >= 65) |
Using & or | between two bool values | invalid operation: operator & not defined on signedIn (variable of type bool). In Go, & and | work only on integers | Use && and || |
Putting the index before the length check, as in roles[0] == "admin" && len(roles) > 0 | The left side runs first, so the empty slice still panics | Put the check that protects the code on the left |
Mixing && and || without parentheses | a || b && c means a || (b && c), which can differ from what you meant | Add parentheses to say which pair goes first |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
invalid operation: ... (mismatched types int and bool) | One side of && or || isn't a bool | Turn it into a comparison |
invalid operation: operator ! not defined on age (variable of type int) | ! stands before a number, or before a comparison without parentheses | Put the comparison in parentheses after ! |
panic: runtime error: index out of range [0] with length 0 | An element was read from an empty slice | Check len(...) > 0 first and join the two with && |
FAQ
Does Go have a & operator for bool values, like Java?
No. In Go, & and | are bitwise operators for integers, so signedIn & !banned doesn't compile. && and || are the only AND and OR for bool values, and both always skip their right side when the left side decides the answer.
Which runs first, && or ||?
&& binds tighter than ||. The specification's Operator precedence lists the comparison operators, then "&& (logical AND), and finally || (logical OR)". So a || b && c means a || (b && c): with a true and b and c false, a || b && c printed true, and (a || b) && c printed false.
Can I use a string or a pointer as a condition, as in JavaScript?
No. A condition in Go must be a bool: if name { ... } with a string, or with a pointer, stops with non-boolean condition in if statement. Compare the value instead, as in name != "" or plan != nil.
Conclusion
&& needs both sides to be true, || needs one, and ! flips a condition. Because && and || stop as soon as the answer is known, a check on the left, such as len(roles) > 0, keeps the code on the right from running when it would panic.
Additional Resources
- Logical operators, Operator precedence and Index expressions, in the Go specification.
