Web Analytics

Constants & Operators

Beginner ~30 min read

Constants are immutable values that cannot be changed after declaration. In this lesson, you'll learn how to define constants, use the powerful iota identifier, and master all of Go's operators.

Constants in Go

Constants are declared using the const keyword. Unlike variables, constants cannot be changed once set:

Output
Click Run to execute your code
Key Points:
  • Constants are declared with const
  • They must be assigned at declaration time
  • Values must be computable at compile time
  • Can be typed or untyped

Typed vs Untyped Constants

// Typed constant
const MaxUsers int = 100

// Untyped constant (more flexible)
const Pi = 3.14159

// Untyped constants can be used with different types
var radius float32 = 5.0
var area = Pi * radius * radius  // Pi adapts to float32
Best Practice: Use untyped constants when possible. They're more flexible and can be used in more contexts without explicit conversion.

The iota Identifier

iota is a special identifier used to create enumerated constants. It starts at 0 and increments by 1 for each constant in a const block:

Output
Click Run to execute your code
How iota Works:
  • Starts at 0 in each const block
  • Increments by 1 for each line
  • Resets to 0 in each new const block
  • Can be used in expressions

Advanced iota Patterns

// Skip values with blank identifier
const (
    _  = iota  // Skip 0
    KB = 1 << (10 * iota)  // 1024
    MB                      // 1048576
    GB                      // 1073741824
)

// Multiple constants per line
const (
    a, b = iota, iota + 10  // 0, 10
    c, d                     // 1, 11
    e, f                     // 2, 12
)

Operators in Go

Go provides a comprehensive set of operators for working with data:

Output
Click Run to execute your code

Arithmetic Operators

Operator Name Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 10 / 3 3 (integer division)
% Modulus 10 % 3 1
++ Increment x++ x = x + 1
-- Decrement x-- x = x - 1
Important: In Go, ++ and -- are statements, not expressions. You can't use them in assignments: y = x++ is invalid!

Comparison Operators

Operator Name Example Result
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true
> Greater than 5 > 3 true
< Less than 5 < 3 false
>= Greater than or equal 5 >= 5 true
<= Less than or equal 5 <= 3 false

Logical Operators

Operator Name Example Description
&& AND true && false true if both are true
|| OR true || false true if at least one is true
! NOT !true Inverts the boolean value

Bitwise Operators

Operator Name Example Description
& AND 5 & 3 Bitwise AND
| OR 5 | 3 Bitwise OR
^ XOR 5 ^ 3 Bitwise XOR
<< Left shift 5 << 1 Shift bits left
>> Right shift 5 >> 1 Shift bits right

Assignment Operators

x = 5      // Simple assignment
x += 3     // x = x + 3
x -= 2     // x = x - 2
x *= 4     // x = x * 4
x /= 2     // x = x / 2
x %=  3    // x = x % 3
x &= 3     // x = x & 3
x |= 3     // x = x | 3
x ^= 3     // x = x ^ 3
x <<= 2    // x = x << 2
x >>= 2    // x = x >> 2

Operator Precedence

When multiple operators appear in an expression, they're evaluated in order of precedence:

Precedence Operators
Highest * / % << >> & &
โ†“ + - | ^
โ†“ == != < <= > >=
โ†“ &&
Lowest ||
Pro Tip: Use parentheses to make your code clearer, even when not strictly necessary: (a + b) * c is more readable than a + b * c.

Common Mistakes

1. Trying to modify constants

// โŒ Wrong
const Pi = 3.14
Pi = 3.14159  // Error: cannot assign to Pi

// โœ… Correct - use variables for changing values
var pi = 3.14
pi = 3.14159  // OK

2. Using ++ or -- in expressions

// โŒ Wrong
x := 5
y := x++  // Error: syntax error

// โœ… Correct
x := 5
x++
y := x  // y = 6

3. Integer division surprise

// โŒ Unexpected result
result := 5 / 2  // result = 2 (not 2.5!)

// โœ… Correct - use float for decimal division
result := 5.0 / 2.0  // result = 2.5
// or
result := float64(5) / float64(2)  // result = 2.5

Exercise: Circle Calculator

Task: Create a program that calculates circle properties.

Requirements:

  • Define a constant for Pi (3.14159)
  • Create a variable for radius (use 5.0)
  • Calculate circumference: 2 ร— ฯ€ ร— r
  • Calculate area: ฯ€ ร— rยฒ
  • Print both results with labels
Show Solution
package main

import "fmt"

const Pi = 3.14159

func main() {
    radius := 5.0
    
    // Calculate circumference
    circumference := 2 * Pi * radius
    
    // Calculate area
    area := Pi * radius * radius
    
    // Print results
    fmt.Printf("Circle with radius %.1f:\n", radius)
    fmt.Printf("Circumference: %.2f\n", circumference)
    fmt.Printf("Area: %.2f\n", area)
    
    // Bonus: Calculate diameter
    diameter := 2 * radius
    fmt.Printf("Diameter: %.1f\n", diameter)
}

Summary

  • Constants are immutable values declared with const
  • Untyped constants are more flexible than typed ones
  • iota creates enumerated constants starting at 0
  • Arithmetic operators: +, -, *, /, %, ++, --
  • Comparison operators: ==, !=, <, >, <=, >=
  • Logical operators: &&, ||, !
  • Bitwise operators: &, |, ^, <<, >>
  • ++ and -- are statements, not expressions in Go

What's Next?

Now that you understand constants and operators, you're ready to learn about Strings. In the next lesson, you'll discover how to work with text data, string operations, and Unicode in Go.