Variables & Types
Dynamic vs static typing
Introduction
In this lesson, you'll learn about variables & types in Go. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with dynamic vs static typing.
Go has its own approach to dynamic vs static typing, which we'll explore step by step.
The Go Way
Let's see how Go handles this concept. Here's a typical example:
package main
var name string = "Alice"
var age int = 30
var score float64 = 9.5
var active bool = true
const Max = 100
// := inside functions — type inferred, fixed
city := "Istanbul" // string, cannot change type
// No reassignment to different type
x := 42
// x = "string" // compile errorComparing to Python
Here's how you might have written similar code in Python:
name = "Alice"
age = 30
score = 9.5
active = True
MAX = 100
# Type hints (optional)
city: str = "Istanbul"
x = 42
x = "can change type" # validYou may be used to different syntax or behavior.
Go types are fixed; Python variables can change type
You may be used to different syntax or behavior.
Go := for short declaration with inference
You may be used to different syntax or behavior.
Go has int and float64; Python has int and float
You may be used to different syntax or behavior.
Go bool: true/false (lowercase); Python: True/False (capitalized)
Step-by-Step Breakdown
1. Fixed Types
Go infers types with :=, but the type is fixed. Unlike Python where x=42 then x='string' is valid.
x = 42; x = "hello" # valid in Pythonx := 42
// x = "hello" // compile error2. bool Literals
Python uses True/False (capitalized). Go uses true/false (lowercase) — a common trip-up when switching.
active = Trueactive := true3. Multiple Assignment
Go supports multiple assignment like Python's tuple unpacking — including the swap idiom.
a, b = b, aa, b = b, a // works in Go too!Common Mistakes
When coming from Python, developers often make these mistakes:
- Go types are fixed; Python variables can change type
- Go := for short declaration with inference
- Go has int and float64; Python has int and float
Key Takeaways
- Go types fixed; Python dynamic
- true/false lowercase in Go; True/False in Python
- := infers type but fixes it
- Multiple assignment (swap) works in both