PY
GO

Python to Go

10 lessons

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

Classes → Structs

Defining data types and behavior

Introduction

In this lesson, you'll learn about classes → structs 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 defining data types and behavior.

GO
In Go:

Go has its own approach to defining data types and behavior, 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

type Person struct {
    Name string
    Age  int
}

func (p Person) Greet() string {
    return "Hi, I'm " + p.Name
}

func (p *Person) Birthday() {
    p.Age++
}

// No inheritance — use embedding
type Employee struct {
    Person      // embedded: Employee has Name, Age, Greet()
    Department string
}

e := Employee{
    Person:     Person{"Alice", 30},
    Department: "Engineering",
}
fmt.Println(e.Greet()) // inherited from Person

Comparing to Python

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

PY
Python (What you know)
from dataclasses import dataclass

@dataclass
class Person:
    name: str
    age: int

    def greet(self) -> str:
        return f"Hi, I'm {self.name}"

    def birthday(self) -> None:
        self.age += 1

class Employee(Person):
    department: str

    def __init__(self, name, age, dept):
        super().__init__(name, age)
        self.department = dept
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go has no classes — structs + methods

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go has no inheritance — use struct embedding (composition)

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

Go pointer receiver (*Person) for mutation; Python mutates via self

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

GO
In Go:

@dataclass auto-generates __init__; Go uses struct literals

Step-by-Step Breakdown

1. Structs vs Classes

Go structs hold data; methods are separate functions with a receiver. Python classes bundle data and methods together.

PY
Python
class Person:
    def __init__(self, name, age): ...
    def greet(self): ...
GO
Go
type Person struct { Name string; Age int }
func (p Person) Greet() string { ... }

2. Pointer Receiver

To modify struct fields in a method, use a pointer receiver (*Person). Python's self always allows mutation.

PY
Python
def birthday(self): self.age += 1
GO
Go
func (p *Person) Birthday() { p.Age++ }

3. Embedding vs Inheritance

Go has no inheritance. Embed a struct to reuse its fields and methods — the embedded type's methods are promoted.

PY
Python
class Employee(Person): pass  # inherits
GO
Go
type Employee struct {
    Person     // promoted — e.Greet() works
    Department string
}

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Go has no classes — structs + methods
  • Go has no inheritance — use struct embedding (composition)
  • Go pointer receiver (*Person) for mutation; Python mutates via self
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

  • Python class → Go struct + receiver methods
  • Python self mutates → Go needs *pointer receiver
  • Inheritance → struct embedding (composition)
  • @dataclass → struct literal initialization
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