Operators
Operators combine or compare values: arithmetic, comparison, logical, bitwise, assignment, and address. Go keeps a small set, requires matching types, and has no ternary and no implicit truthiness.
Detailed Explanation
You write n + 1, err != nil, and p&Write != 0 every day. Operators in Go look familiar if you know C. The differences that matter are the ones that remove silent bugs: both sides of + or == must be the same type, && and || work only on bool, and there is no === because there is no coercion.
Arithmetic and overflow. + - * / % work on integers and floats (% is integers only). Integer overflow wraps. Integer division truncates toward zero: -3 / 2 is -1. Dividing by zero panics for integers and yields +Inf or NaN for floats. There is no **; use math.Pow or a shift for powers of two (1 << n).
Comparison. == != < <= > >= compare ordered types. Slices, maps, and functions are comparable only to nil. Structs and arrays compare field by field if every field is comparable. Interfaces compare by dynamic type and value; comparing two interfaces that both hold incomparable concrete values panics at runtime.
Logical operators short-circuit. a && b does not evaluate b when a is false. a || b does not evaluate b when a is true. Both sides must be bool. Bitwise | and ^ are not logical or.
Bitwise. & | ^ &^ << >> operate on integers. ^ is both bitwise complement (unary) and XOR (binary). &^ is "AND NOT," the bit-clear operator: p &^ Write turns the Write bit off. Shifts: the left side is the value, the right side is the count and must be unsigned or an untyped integer that is not negative.
Address and indirection. &x is the address of x. *p is the value p points at. <-ch receives. ch <- v sends. <- is an operator, not a function.
Assignment. = += -= and the rest update an existing name. ++ and -- are statements, not expressions: n++ is legal, f(n++) is not. There is no n = m++.
What it costs. Operators compile to the obvious machine instructions. The cost you actually pay is a panic: nil pointer dereference, integer divide by zero, send on a closed channel, compare of incomparable interface values. Prefer errors.Is over == on errors, because wrapped errors are not equal to their sentinel by ==.
Code Examples
package main
import "fmt"
func main() {
n := 7
fmt.Println(n%2 == 1) // true, odd
fmt.Println(n<<1) // 14
fmt.Println(n&^1) // 6, clear the low bit
ok := n > 0 && n < 10
fmt.Println(ok)
}func pointers() {
x := 1
p := &x
*p = 2
fmt.Println(x) // 2
}type Point struct{ X, Y int }
func compare() {
fmt.Println(Point{1, 2} == Point{1, 2}) // true
var s []int
fmt.Println(s == nil) // true
// fmt.Println(s == []int{}) // compile error: slices are not comparable
}Common Gotchas
Different named types cannot use `+` or `==` without conversion
`type Count int` does not add to `int`.
`++` is a statement
it cannot sit inside a larger expression.
Interface `==` can panic
if both operands are non-nil interfaces holding slices, maps, or functions, the comparison panics. Use a type switch or `reflect.DeepEqual` in tests.
There is no ternary operator
write an `if` or a small helper that returns the value.