← Back to list

Learn OOP in Go from Someone who migrated all his OOP knowledge from Dotnet to Go

Myth 1: Go doesn’t support OOP principles

Md. Sayeed Rahman · 2026-06-14 15:00 · 0 claps · 2.4 min read
#golang #oop-in-go #multiple-inheritance #oops-interview-questions
Open on Medium ↗

Learn OOP in Go from Someone who migrated all his OOP knowledge from Dotnet to Go

Myth 1: Go doesn’t support OOP principles

Coming from a Dotnet background, I was successful practicing most of my OOP knowledge of Dotnet into Go that I often use in day-to-day software development.

The rest? Okay, not all textbook contepts we need equally while building real software.

Myth 2: Go only supports Composition, not Inheritance

Truth: Go supports both inheritance & composition. It’s even better, it solves diamond problem.

Let’s Go To Practice

First create a new go project, define a parent schema & add one method.

package main

type Parent struct {
    Name     string
    Property string
}

func (p *Parent) HasProperty() bool {
        return len(p.Property) > 0
}

Then write a main function to test its behavior.

func main() {
    parent := Parent{Name: "Parent Name", Property: "Some Property"}
    println(parent.Name)          // Output: Parent Name
    println(parent.HasProperty()) // Output: true
}

Inheritance

Now define a child schema. Add this code below the parent schema definition to inherit it.

type Child struct {
    Parent
}

Notice here, we are not saying Parent Parent, we are saying Parent.

In Golang, when we write,

type Child struct {
    P Parent // composition
}

it becomes a HAS-A relationship. Child contains a prop P of type Parent.

But, when we write,

type Child struct {
    Parent // inherits directly
}

it becomes a IS-A relationship. Child implicitly inherits all properties and methods of Parent.

Now create a instance of child in main function and see you can access all properties & methods of the parent from it.

Polymorphism > Method Overriding

Now, let’s override property of Parent in Child.

Add the below code under Child struct.

func (c *Child) HasProperty() bool {
    return false // consider child does not have any property yet
}

And add the below code in main function.

child := Child{Parent: parent}
child.Name = "Child"         // This will override the Name field of Parent
println(child.Name)          // Output: Child
println(child.HasProperty()) // Output: false (calls overriden method, not from base struct)

Go is not Traditional OOP Language

When people say this, they mean GoLang doesn’t 100% mirror to other OOP languages.

For instance, in Go, method overloading is not supported.

But that limitation doesn’t restrict us appyling OOP paradigms we need in day-to-day engineering.

Rather, Go is designed to perform even better than most OOP languages!

Go allows multi-inheritance, what Dotnet, Java, PHP don’t!

In Dotnet, Java — you can inherit multiple interfaces, but at most one class only.

But in Go, we can inherit as much classes (structs) as we want via struct embedding.

Modify the above code to this -

type Parent struct {
    Name     string
    Property string
}

func (p *Parent) HasProperty() bool {
       return len(p.Property) > 0
}

type Student struct {
       Class string
       Roll  int
}

// This code below demonstrates multiple inheritance in GoLang
type Child struct {
       Parent  // Child IS-A Parent
       Student // Child IS-A Student
}

Now inside main function, try accessing the properties of both Parent & Student from an instance of Child.


메타데이터
post_id
cd4357550faa
slug
learn-oop-in-go-from-someone-who-migrated-all-his-oop-knowledge-from-dotnet-to-go-cd4357550faa
url
https://medium.com/@sayeedrahman_67698/learn-oop-in-go-from-someone-who-migrated-all-his-oop-knowledge-from-dotnet-to-go-cd4357550faa
canonical_url
https://medium.com/@sayeedrahman_67698/learn-oop-in-go-from-someone-who-migrated-all-his-oop-knowledge-from-dotnet-to-go-cd4357550faa
author_url
https://medium.com/@sayeedrahman_67698
status
ok
fetched_at
2026-06-20 20:29:01