PY
GO

Python to Go

10 lessons

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

Modules → Packages

Organizing code across files

Introduction

In this lesson, you'll learn about modules → packages 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 organizing code across files.

GO
In Go:

Go has its own approach to organizing code across files, 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
// mathutil/mathutil.go
package mathutil

const PI = 3.14159

// Exported (capital letter)
func Add(a, b int) int { return a + b }

// Unexported (lowercase) — like Python's _helper
func helper() {}

// main.go
package main

import (
    "fmt"
    "github.com/user/app/mathutil"
)

func main() {
    fmt.Println(mathutil.Add(2, 3))
    fmt.Println(mathutil.PI)
}

// go.mod + go get for dependencies

Comparing to Python

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

PY
Python (What you know)
# mathutil.py
PI = 3.14159

def add(a: int, b: int) -> int:
    return a + b

def _helper():  # private by convention
    pass

# main.py
from mathutil import add, PI
import mathutil

result = add(2, 3)
print(mathutil.PI)

# requirements.txt / pyproject.toml for deps
# pip install requests
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go exports by capitalization; Python uses _ prefix convention

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go imports by package path; Python imports by module name

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

go.mod replaces requirements.txt/pyproject.toml

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

go get replaces pip install

Step-by-Step Breakdown

1. Export Control

Go uses capitalization: capital = exported (public), lowercase = unexported (package-private). Python uses _ by convention.

PY
Python
def add(): ...  # public
def _helper(): ...  # private by convention
GO
Go
func Add() {}   // exported
func helper() {} // unexported

2. Package Paths

Go imports use full module paths from go.mod. Python imports use relative or absolute module names.

PY
Python
from mathutil import add
GO
Go
import "github.com/user/app/mathutil"
mathutil.Add(2, 3)

3. go.mod vs requirements.txt

go.mod defines the module path and dependencies with exact versions. 'go get' installs packages like pip install.

Rule of Thumb
Run 'go mod tidy' after adding imports — it updates go.mod and go.sum automatically.

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Go exports by capitalization; Python uses _ prefix convention
  • Go imports by package path; Python imports by module name
  • go.mod replaces requirements.txt/pyproject.toml
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

  • Capital = exported; lowercase = unexported
  • Package paths vs module names for imports
  • go.mod = requirements.txt + setup.py
  • go get = pip install
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in Go to practice these concepts.
PreviousNext