PY
GO

Python to Go

10 lessons

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

Functions

Functions and error handling

Introduction

In this lesson, you'll learn about functions 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 functions and error handling.

GO
In Go:

Go has its own approach to functions and error handling, 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

import (
    "errors"
    "fmt"
)

func add(a, b int) int { return a + b }

// No default params
func greet(name string) string { return "Hello, " + name + "!" }

// Multiple return values (like Python tuple return)
func minMax(nums []int) (int, int) {
    lo, hi := nums[0], nums[0]
    for _, n := range nums {
        if n < lo { lo = n }
        if n > hi { hi = n }
    }
    return lo, hi
}

lo, hi := minMax([]int{3, 1, 4, 1, 5})

// Returns error instead of raising
func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("division by zero")
    }
    return a / b, nil
}

Comparing to Python

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

PY
Python (What you know)
def add(a: int, b: int) -> int:
    return a + b

def greet(name: str = "World") -> str:
    return f"Hello, {name}!"

def minmax(nums: list[int]) -> tuple[int, int]:
    return min(nums), max(nums)

lo, hi = minmax([3, 1, 4, 1, 5])

# Raises on error
def divide(a: float, b: float) -> float:
    if b == 0:
        raise ValueError("division by zero")
    return a / b
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go returns errors as values; Python raises exceptions

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go multiple return values mirror Python tuple returns

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go has no default parameters; Python does

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go error must be checked at call site; Python exceptions can propagate

Step-by-Step Breakdown

1. Error Values vs Exceptions

Go's most important difference: errors are return values, not exceptions. Every call site must handle the error explicitly.

PY
Python
try:
    r = divide(a, b)
except ValueError: ...
GO
Go
r, err := divide(a, b)
if err != nil { /* handle */ }

2. Multiple Returns

Go's multiple return values work just like Python's tuple returns — the calling syntax is similar.

PY
Python
def minmax(nums): return min(nums), max(nums)
lo, hi = minmax(nums)
GO
Go
func minMax(nums []int) (int, int) { ... }
lo, hi := minMax(nums)

3. No Default Parameters

Go has no default parameter values. Use separate functions or the functional options pattern for configurable behavior.

PY
Python
def connect(host="localhost", port=8080): ...
GO
Go
func connect(host string, port int) { ... }
func connectDefault() { connect("localhost", 8080) }

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Go returns errors as values; Python raises exceptions
  • Go multiple return values mirror Python tuple returns
  • Go has no default parameters; Python does
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 returns errors; Python raises exceptions
  • Multiple returns like Python tuple returns
  • No default parameters in Go
  • nil is Go's None for error checks
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