Golang Class and Object Explained: 7 Practical Examples to Learn Fast!

golang class

Ever tried explaining golang class and object concepts to someone new to Go? It’s like trying to describe how coffee works without mentioning caffeine.
When I first started learning Golang, I came from a background in languages like Java and Python — both of which revolve around classes and objects. But Go? Oh boy, it plays by its own rules.

In this post, I’ll break down Golang classes, objects, and how Go manages to stay object-oriented without actually having classes. If you’ve been scratching your head over how Go handles object-like behavior, I promise you’ll get it by the end.

What Exactly Is a “Class” in Golang?

Let’s get this straight: Golang doesn’t have classes — at least not the way Java, C++, or Python do.
When I first found out, I was confused. “Wait, how can a language call itself object-oriented without classes?”

Here’s the deal: Go replaces classes with structs (short for structures).
Structs let you group together data fields and associate methods with them — so you can simulate the behavior of classes.

package main
import "fmt"

type Car struct {
Brand string
Year int
}

func (c Car) Details() {
fmt.Println("Car:", c.Brand, "Year:", c.Year)
}

func main() {
myCar := Car{Brand: "Tesla", Year: 2025}
myCar.Details()
}
Go

When I first wrote this, I realized something fascinating: the struct acts like a class, and myCar acts like an object.
So even though Go avoids the word “class,” it achieves the same thing.

Objects in Golang (How They Actually Work)

Now that we have a struct, we can create an object — in Go terms, an instance of a struct.
In the example above, myCar is the object of the Car struct.

If you’ve coded in Java, this is like doing:

Car myCar = new Car("Tesla", 2025);
Java

But Go doesn’t use new for everything. You can simply assign values directly. It’s clean and minimal — and I love that.

Objects in Go don’t need inheritance. Instead, Go prefers composition. That means instead of saying “this class extends that class,” we just embed one struct inside another.

Struct Embedding: Go’s Secret Sauce

Here’s where golang class magic truly happens — struct embedding.

Think of embedding as Go’s way of saying, “You can reuse stuff, but keep it simple.”
Let’s say we have a basic struct for an engine:

type Engine struct {
Horsepower int
}
func (e Engine) Start() {
fmt.Println("Engine starting with", e.Horsepower, "HP")
}
Go

Now, you can embed that into another struct:

type Car struct {
Brand string
Engine
}
Go

Boom 💥 — now Car has all the properties of Engine.
This means Car can call the Start() method directly.

myCar := Car{Brand: "BMW", Engine: Engine{Horsepower: 400}}
myCar.Start()
Go

See what just happened? Composition replaced inheritance — one of Go’s most elegant design decisions.

🔄 Methods and Receivers: Go’s Version of Class Functions

One thing I admire about golang class design is how it handles methods.
You don’t need to define them inside a class. Instead, you attach them to a type using receivers.

In our previous example, the func (c Car) Details() part? That’s the method attached to the Car struct.

This makes your code modular, clean, and predictable.
You can even use pointer receivers if you want to modify data:

func (c *Car) UpdateYear(year int) {
c.Year = year
}
Go

Now, when you call myCar.UpdateYear(2026), it’ll actually change the Year field.

That’s Go’s way of saying — “I may not have classes, but I’ve got class.

Encapsulation in Golang (Yes, It Exists!)

People often ask me, “Does Go support encapsulation?”
And my answer is always a confident yes.

In golang class terms, encapsulation happens through exported and unexported fields.
In Go, if a field name starts with a capital letter, it’s public (exported).
If it starts with a lowercase letter, it’s private (unexported).

Example:

type user struct {
name string // private
Age int // public
}
Go

Interfaces in Golang: The Hidden Power

If structs are Go’s version of classes, interfaces are its version of polymorphism.
They let different structs share the same behavior — without forcing them to belong to the same class hierarchy.

Example:

type Animal interface {
Speak() string
}

type Dog struct{}
func (d Dog) Speak() string {
return "Woof!"
}

type Cat struct{}
func (c Cat) Speak() string {
return "Meow!"
}

func main() {
animals := []Animal{Dog{}, Cat{}}
for _, a := range animals {
fmt.Println(a.Speak())
}
}
Go

That’s polymorphism in action — the Go way.
No inheritance, no class trees — just clean, readable code.

If you’re into design principles, check out Go’s official interface guidelines. It’s a goldmine.

Why Golang Avoided Traditional Classes

When I read the Go team’s philosophy, something clicked: Go wasn’t built to imitate Java or Python.
It was built to simplify how we structure code.

Go focuses on:

  • Composition over inheritance
  • Interfaces over class hierarchies
  • Simplicity over complexity

In short: Go doesn’t have classes, because it doesn’t need them.

Real-Life Example: Building a Mini Golang App

Here’s a quick one — I built a tiny program to manage books in a library

type Book struct {
Title string
Author string
}

func (b Book) Info() {
fmt.Println("Title:", b.Title, "| Author:", b.Author)
}

func main() {
myBook := Book{Title: "The Go Programming Language", Author: "Alan Donovan"}
myBook.Info()
}
Go

It’s small but powerful. Structs, methods, and objects — all working seamlessly.

If you ever want to go deeper, check out Go by Example. It’s my go-to site when I get stuck.

Final Thoughts:

After years of coding in different languages, I’ve come to appreciate Go’s simplicity.
Sure, it doesn’t use “class” and “object” the same way as Java or Python — but that’s what makes it special.

You’ll still get all the benefits of object-oriented programming: encapsulation, polymorphism, and reusable code.
You just get them in Go’s elegant, minimalist style.

If you’re new to Go, remember:
👉 A struct is your “class.”
👉 Its instance is your “object.”
👉 And methods are your “class functions.”

Once you think like that, everything falls into place.

0 Shares:
You May Also Like