Polymorphism in Go lang

Alfred is a software engineer and cyber security specialist based in Enugu, Nigeria. He is passionate about solving real-world problems through well-crafted software and leveraging blockchain technology.
Search for a command to run...

Alfred is a software engineer and cyber security specialist based in Enugu, Nigeria. He is passionate about solving real-world problems through well-crafted software and leveraging blockchain technology.
No comments yet. Be the first to comment.
The tech industry is changing fast—but not in the way most people think. By 2026, success in engineering won’t be about chasing the newest tools or stacking buzzwords on a résumé. It will be about depth, ownership, and usefulness. Here’s a grounded l...

1. Server Streaming RPC: Description: In this type of RPC, the client sends a single request to the server, and the server responds with a stream of messages. The client reads from this stream of responses until there are no more messages. Use Case...
Email communication is a crucial aspect of many applications, and Amazon Simple Email Service (SES) provides a reliable and scalable solution for sending emails in the Amazon Web Services (AWS) environment. I've created a go package that facilitates ...

Introduction This article shows a complete setup on how you can verify a signature sent by paystack when they make a POST request to your server. I'm using the bun http router as my go lang framework. I really do not have strength for long talks, so ...
Funny title, but that's the reality from January to December. So, you're here to hear some gist about my 2023, say less. First I just want to take a moment to laugh small, 😂😂😂😂 why? the plans I had for 2023 come be like say na small pikin write a...

Polymorphism is the ability of an object or function to take on multiple forms. In the Go programming language, polymorphism is supported through the use of interfaces.
An interface in Go defines a set of methods that a type must implement to satisfy the interface. This means that any type that implements the methods defined in an interface is said to implement the interface.
For example, suppose we have an interface called Shape that defines the methods Area() and Perimeter() for calculating the area and perimeter of a shape. We could define the Shape interface like this:
type Shape interface {
Area() float64
Perimeter() float64
}
Now, any type that implements the Area() and Perimeter() methods can be said to implement the Shape interface. For example, we could define a Rectangle type that satisfies the Shape interface like this:
type Rectangle struct {
width float64
height float64
}
func (r *Rectangle) Area() float64 {
return r.width * r.height
}
func (r *Rectangle) Perimeter() float64 {
return 2 * (r.width + r.height)
}
Here, the Rectangle type implements the Area() and Perimeter() methods, so it satisfies the Shape interface. This means that we can use a Rectangle value wherever a Shape is expected, and the appropriate method will be called automatically.
In this way, polymorphism in Go allows us to write code that can work with multiple types in a consistent manner, without needing to know the specific type of an object at compile time.
package main
import "fmt"
type person struct{
firstname string;
lastname string
}
type human interface{
talk()
}
func (p person) talk(){
fmt.Println("My name is", p.firstname)
}
func main(){
person1 := person{
firstname: "Alfred",
lastname: "Johnson-awah",
}
person1.talk()
}
In the code above we created a person struct which has the firstname and lastnameproperties. To showcase polymorphism, we created an interface with a function of talk. Lastly, we have implemented the talk function and attached it to a struct type of person . This means that any identifier of type person would always have a method talk and at the end of the day, we can boldly say that the identifier (person1 in our case) is of both person and human type, this is polymorphism.
You can still go ahead and add more complex code by implementing type assertion using a switch statement as seen below.
package main
import "fmt"
type person struct {
firstname string
lastname string
}
type seniorMan struct {
person
hasACar bool
}
type human interface {
talk()
}
func (p person) talk() {
fmt.Println("My name is", p.firstname)
}
func (p seniorMan) talk() {
fmt.Println("I am a person and senior man, see my ID", p, "and i have a car too", p.hasACar)
}
func resolveType(h human) {
switch t := h.(type) {
case person:
fmt.Println("This is a normal person", t.firstname)
case seniorMan:
fmt.Println("This is a senior man, does this person have a car?", t.hasACar)
}
}
func main() {
person1 := person{
firstname: "Alfred",
lastname: "Johnson-awah",
}
seniorMan1 := seniorMan{
person: person{
firstname: "Joe",
lastname: "Biden",
},
hasACar: true,
}
resolveType(seniorMan1)
resolveType(person1)
person1.talk()
}
Running this code on the console, your output should be like the below:
This is a senior man, does this person have a car? true
This is a normal person Alfred
My name is Alfred
The seniorMan struct type inherits properties of the person struct type with an additional property hasACar . The most important exerpt of this code is the resolveType function which uses a switch state to person type assertion on interfaces to find out their underlying types and execute the correct block of code.
In this blog post, we learnt about polymorphism and how it stems from interface implementation.