Operators in Go
Complete guide to Go operators: arithmetic, assignment, comparison, logical, bitwise, and operator precedence. Go has no ternary or optional chaining; use if and nil checks instead.
Introduction to Operators
Operators are symbols that perform operations on one or more values (operands). They are the building blocks of expressions and logic in Go.
1. What are Operators?
Operators work on operands to produce a value.
Arithmetic Operators
Arithmetic operators work on numeric types. For integers, / is integer division (truncates toward zero) and % is the remainder. For floats, / is normal division.
1. + - * / %
Basic arithmetic. With integers, / truncates; use float for decimal division.
2. Increment and Decrement
Go has ++ and -- only as statements, not expressions. They do not return a value.
Assignment Operators
= assigns a value. Compound operators +=, -=, *=, /= combine an operation with assignment.
1. = Assignment
Assign a value from right to left.
2. Compound Assignment (+=, -=, *=, /=)
x += 5 is equivalent to x = x + 5.
Comparison Operators
Go has == and != for equality, and <, <=, >, >= for ordering. Types must match; there is no automatic type coercion.
1. == and !=
Equality and inequality. Types must be the same.
2. < > <= >=
Relational comparisons. Used in conditions and sorting.
Logical Operators
&& is logical AND, || is logical OR, ! is logical NOT. They short-circuit: the right side is not evaluated if the result is already determined.
1. && || !
Logical AND, OR, NOT. Operands must be bool.
2. Short-circuit Behavior
If the left side of && is false, the right is not evaluated. If the left of || is true, the right is not evaluated.
String Operators
Go supports + for string concatenation and comparison operators (==, !=, <, <=, >, >=) for lexicographic ordering. For building large strings efficiently, use strings.Builder.
1. String Concatenation (+)
Use + to join strings.
2. String Comparison (==, !=, <, <=, >, >=)
Strings are comparable; ordering is lexicographic (byte-wise).
Inspecting Types
Go has no built-in type-of operator. Use fmt.Printf("%T", x) or the reflect package. Type assertions (x.(T)) are used to get a concrete type from an interface value.
1. Format Verb %T
Print the type of a value with %T.
2. Type Assertions
Use a type assertion v.(Type) to get a concrete type from an interface. Check with the two-value form.
Defaults and Safe Access
For "use default if zero", use an if check. For "safe field access" on pointers or interfaces, check for nil before dereferencing.
1. Default When Zero or Nil
Use if to assign a default when a value is zero or nil.
2. Safe Field Access (Nil Check)
Check for nil before accessing pointer or interface fields.
Bitwise Operators
Bitwise operators work on integer types: & (AND), | (OR), ^ (XOR), &^ (bit clear), << (left shift), >> (right shift). Go uses two's complement for signed integers.
1. Binary Representation
Understanding bits helps with bitwise operators. Integers are stored in two's complement.
2. Bitwise AND (&)
Result bit is 1 only if both bits are 1.
3. Bitwise OR (|)
Result bit is 1 if at least one bit is 1.
4. Bitwise XOR (^)
Result bit is 1 when the two bits are different.
5. Left Shift (<<)
Shift bits left; effectively multiply by powers of 2.
6. Right Shift (>>)
Shift bits right; for signed integers the sign bit is preserved.
7. Practical Use Cases
Even/odd check and fast multiply/divide by 2.
Operator Precedence
Multiplication and division have higher precedence than addition and subtraction. Use parentheses to make order explicit.
1. Precedence Example
* and / run before + and -. Use () to override.
2. Logical Operator Precedence
&& has higher precedence than ||.
3. Operator Precedence Table
Higher precedence operators are evaluated first. When in doubt, use parentheses.
Combining Slices (append and ...)
To combine slices or add elements, use append(slice, elements...). The ... after a slice expands it into individual arguments.
1. Appending a Slice to a Slice
Use append(s, s2...) to add all elements of s2 to s.
Conditional Struct Fields
Go has no object spread or conditional spread. Build a struct and set fields in if branches, or use a constructor that takes options.
1. Conditional Field Assignment
Set a field only when a condition is true.