PY
GO

Python to Go

10 lessons

Progress0%
1Variables & Types2Functions3Lists → Slices, Dicts → Maps4Classes → Structs5asyncio → Goroutines6Modules → Packages7Interfaces8Error Handling Patterns9Testing10Standard Library
All Mirror Courses
PY
GO
Variables & Types
MirrorLesson 1 of 10
Lesson 1

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.

Mirror Card
PY
From Python:

In Python, you're familiar with dynamic vs static typing.

GO
In Go:

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:

GO
Go 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 error

Comparing to Python

Here's how you might have written similar code in Python:

PY
Python (What you know)
name = "Alice"
age = 30
score = 9.5
active = True

MAX = 100

# Type hints (optional)
city: str = "Istanbul"

x = 42
x = "can change type"  # valid
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go types are fixed; Python variables can change type

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go := for short declaration with inference

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go has int and float64; Python has int and float

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

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.

PY
Python
x = 42; x = "hello"  # valid in Python
GO
Go
x := 42
// x = "hello" // compile error

2. bool Literals

Python uses True/False (capitalized). Go uses true/false (lowercase) — a common trip-up when switching.

PY
Python
active = True
GO
Go
active := true

3. Multiple Assignment

Go supports multiple assignment like Python's tuple unpacking — including the swap idiom.

PY
Python
a, b = b, a
GO
Go
a, 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
Common Pitfall
Don't assume Go works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in Go to practice these concepts.
OverviewNext