Code-Memo

Operations

Go provides arithmetic, comparison, logical, bitwise, and assignment operators. There is no implicit truthiness: conditions must be boolean.

Arithmetic

+, -, *, /, % for integers; +, -, *, / for floats. Integer division truncates toward zero.

a := 7 / 3    // 2
b := 7.0 / 3  // 2.333...
Comparison

==, !=, <, <=, >, >=. 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 {
	// ...
}
Logical

&&, ||, ! — operands must be bool.

Bitwise

&, |, ^, &^, <<, >> on integers.

Address-of and indirection

&x takes the address of x; *p dereferences pointer p.

Short declaration redeclaration

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
Increment/decrement

i++ and i-- are statements, not expressions; only postfix form exists.

i++
String concatenation

Use + for a few pieces; prefer strings.Builder or fmt.Sprintf for many concatenations.

s := "a" + "b"