Constants & Operators
Constants in Go are compile-time values declared with const.
Introduction
Constants in Go are compile-time values declared with const. Combined with iota, they create elegant enumerations for HTTP status codes, error codes, and state machines. Operators follow familiar arithmetic, comparison, and logical rules with Go's strict typing.
Understanding operator precedence, short-circuit evaluation with && and ||, and the difference between == and deep equality for slices (via slices.Equal or reflect) prevents subtle comparison bugs in API handlers and test assertions.
This lesson builds the expression vocabulary you will use in every conditional, loop, and business rule throughout the course — from discount calculations to permission checks in middleware.
The story
A Cloudflare Workers routing configurator maps HTTP status codes and priority tiers to upstream pool weights. Constants like StatusBadGateway = 502 and bit-mask flags for feature toggles keep magic numbers out of the codebase — when an on-call engineer reads the diff at 3 AM, named constants explain intent instantly.
Operators combine arithmetic for rate limiting (requests * windowSeconds), comparisons for threshold alerts, and logical AND for feature flags — the glue between metrics and automated remediation.
Understanding the topic
Key concepts
- const declares immutable values; typed and untyped constants exist.
- iota resets to 0 in each const block and increments per line — ideal for enums.
- Arithmetic: + - * / % with integer division truncating toward zero.
- Comparison: == != < <= > >= — struct equality compares fields if comparable.
- Logical: && || ! short-circuit; bitwise: & | ^ << >> &^.
- Untyped constants participate in flexible arithmetic until assigned to typed variable.
Step-by-step explanation
- Declare constants individually or in const ( ) blocks.
- iota starts at 0; each subsequent line increments unless reset with new const block.
- Expressions in const must be compile-time computable.
- Operators apply to typed operands; mixing types requires conversion.
- && stops if left is false; || stops if left is true.
- Bitwise ops work on integer types for flags and permissions.
Practical code example
HTTP-style status constants with iota and operator usage in authorization checks:
package mainimport "fmt"type Role intconst (RoleGuest Role = iotaRoleUserRoleAdmin)const (PermRead = 1 << iota // 1PermWrite // 2PermDelete // 4)func hasPermission(granted, required int) bool {return granted&required == required}func main() {role := RoleAdminperms := PermRead | PermWritefmt.Printf("role=%d admin=%v read=%v write=%v delete=%v\n",role, role == RoleAdmin,hasPermission(perms, PermRead),hasPermission(perms, PermWrite),hasPermission(perms, PermDelete))}
Output
role=2 admin=true read=true write=true delete=false
Line-by-line code explanation
type Role intdefines a new type, Role, whose underlying type is int, so roles are not accidentally mixed with plain integers.RoleGuest Role = iotastarts a constant group. iota counts from 0 inside a const block, so RoleGuest is 0.RoleUserhas no expression of its own, so it repeats the previous line, giving it type Role and the next iota value, 1. RoleAdmin on the following line is 2.PermRead = 1 << iotashifts 1 left by iota bits in a new const group; with iota at 0, PermRead is 1. PermWrite and PermDelete repeat the expression and become 2 and 4, one bit each.return granted&required == requireduses bitwise AND to keep only the bits both numbers share, then compares with required. It is true only when every bit in required is set in granted.role := RoleAdminstores the constant in a variable; its type is Role.perms := PermRead | PermWritecombines the two permission bits with bitwise OR, creating a value that has both set.role, role == RoleAdmin,passes the role itself and the result of comparing it with RoleAdmin as arguments to the Printf call that starts above.hasPermission(perms, PermDelete))checks the delete bit, which was never added to perms. The other two checks above it use the same function for PermRead and PermWrite.
Key takeaway: iota enums are idiomatic Go — no separate enum keyword. Bit flags with iota and bitwise ops model RBAC permissions cleanly.
Real-world use
Where you'll use this in production
- HTTP status codes and error code enumerations in API packages.
- State machines for order lifecycle (Pending, Shipped, Delivered).
- Feature flags and permission bitmasks in authorization middleware.
- Time constants (time.Second, time.Minute) built with typed duration arithmetic.
Best practices
- Use iota for related constant groups; add String() method for debug output.
- Name constants in PascalCase if exported, camelCase if package-private.
- Use typed constants (const StatusOK = 200) for API clarity.
- Prefer const over var for values that never change.
- Document iota blocks with a comment on the first line for go doc.
- Use parentheses in complex expressions for readability.
Common mistakes
- Assuming iota continues across separate const blocks — it resets.
- Using == to compare slices — compares pointers, not contents.
- Integer division surprise: 5/2 is 2, not 2.5 — use float64 conversion.
- Modifying const — compile error, but trying via pointer tricks fails.
- Confusing & (address) with & (bitwise AND) depending on context.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhat is iota?+
Answer
2BeginnerQuestionShort-circuit evaluation?+
Answer
3IntermediateQuestionTyped vs untyped constants?+
Answer
4IntermediateQuestionCompare two slices for equality?+
Answer
5AdvancedQuestionDesign permission system with iota bit flags.+
Answer
Summary
const and iota create clean enumerations and bit flags. Operators require matching types; conversions are explicit. Short-circuit && and || enable safe nil-check chains. Never use == for slice content comparison — use slices.Equal. Next lesson: formatted I/O with fmt, bufio, and os packages.