Go has picked some good concepts from different kind of programming languages like procedural, functional and object-oriented programming language and combined them together to create a unique flavor programming language.

Go isn’t an object-oriented language like C++, Java, C# etc. but it supports some of the OOPs concepts like Encapsulation, Type Methods, Composition etc.

In this blog post, we will see how can we implement those concepts in Go

Type Methods Link to heading

Go does not have classes but it has types. Structs are user-defined types in Go. Structs with methods serve the same purpose as classes in other programming languages.

package main

import (
	"fmt"
)

//Person struct to store person information
type Person struct {
	FirstName string
	LastName  string
}

func (p Person) getFullName() string {
	return p.FirstName + " " + p.LastName
}

func main() {
	p := Person{FirstName: "John", LastName: "Doe"}
	fmt.Printf("%s\n", p.getFullName())
}

Encapsulation Link to heading

Go support encapsulation at the package level. Any struct, variable, interface, type, function etc. whose name starts with a small letter is private to that package and anything that starts with a capital letter is public and can be used in other packages.

Composition Link to heading

Composition is an act of including one structure into another which is sometimes called as trait or mixin in other languages. Go does not support inheritance but it provides implicit composition using the struct. Composition over inheritance is an old battle and Go is the first language that takes a firm stand on that.

The Student struct will have a field of the type Person. Because we didn’t give it an explicit field name, we can implicitly access the fields and functions of Person type.

package main

import (
	"fmt"
)

//Person struct to store person information
type Person struct {
	FirstName string
	LastName  string
}

func (p Person) getFullName() string {
	return p.FirstName + " " + p.LastName
}

//Student struct to represent student
type Student struct {
	Person     //A `Person` type field without any name
	University string
}

func main() {
	p := Person{FirstName: "John", LastName: "Doe"}
	s := Student{Person: p, University: "Stanford"}
	fmt.Printf("First Name: %s\n", s.FirstName)
	fmt.Printf("Last Name: %s\n", s.LastName)
	fmt.Printf("Full Name: %s\n", s.getFullName())
}

Overloading Link to heading

Simply, Go does not support overloading. We can not have more than one methods functions with same name and different argument or return type. For this reason, you may end up writing different functions like GetByID, GetByName etc.

However, because of implicit composition, we can “overwrite” the methods of composed types.

For example, Student struct can have its own introduce method. We can always access the composed method like s.p.introduce().

package main

import (
	"fmt"
)

//Person struct to represent a person
type Person struct {
	FirstName string
	LastName  string
}

func (p Person) getFullName() string {
	return p.FirstName + " " + p.LastName
}

func (p Person) introduce() string {
	return fmt.Sprintf("Hi !! My name is %s %s.", p.FirstName, p.LastName)
}

//Student struct to represent a student
type Student struct {
	Person     //A `Person` type field without any name
	University string
}

func (s Student) introduce() string {
	return fmt.Sprintf("Hi !! My name is %s %s. I am student at %s.", s.FirstName, s.LastName, s.University)
}

func main() {
	p := Person{FirstName: "John", LastName: "Doe"}
	s := Student{Person: p, University: "Stanford"}
	fmt.Printf("Student Introduction: %s\n", s.introduce())
	fmt.Printf("Person Introduction: %s\n", s.Person.introduce())
}

Go isn’t an object-oriented programming language like C++, Java, C# etc. It does not have object and neither inheritance. It also does not support other important object-oriented concepts like polymorphism and overloading.

It does have struct which are similar to class. We can write type methods for struct. It also supports encapsulation at the package level. Go prefers composition over inheritance.