Go provides arithmetic, comparison, logical, bitwise, and assignment operators. There is no implicit truthiness: conditions must be boolean.
+, -, *, /, % for integers; +, -, *, / for floats. Integer division truncates toward zero.
a := 7 / 3 // 2
b := 7.0 / 3 // 2.333...
==, !=, <, <=, >, >=. Two values are comparable only when allowed by the language spec (slices, maps, and functions are not comparable with == except nil).
if name == "go" && version >= 1.21 {
// ...
}
&&, ||, ! — operands must be bool.
&, |, ^, &^, <<, >> on integers.
&x takes the address of x; *p dereferences pointer p.
In a multi-variable :=, at least one variable must be new in that block; others may be assigned.
err := do()
result, err := doAgain() // ok if result is new
i++ and i-- are statements, not expressions; only postfix form exists.
i++
Use + for a few pieces; prefer strings.Builder or fmt.Sprintf for many concatenations.
s := "a" + "b"