Introduction
Go is a general-purpose programming language with a focus on systems programming. It is strongly typed, garbage-collected, and supports concurrent programming explicitly. Packages are the building blocks of programs, and their attributes allow for efficient dependency management. Google engineers created Go to construct stable and efficient software.
This article will give you enough information on polymorphism using interfaces in Go to let you go to higher levels of expertise. Before reading this article, you should have a basic understanding of computer programming terms. It will be pretty simple for you to grasp the principles if you have a decent command of C. If that is not the case, you can return here after reading our Introduction to C article.
Polymorphism in GoLang
Polymorphism refers to a message's ability to appear in multiple forms. A lady, for example, can have various characteristics at the same time. As a mother, wife, sister, employee, and so forth. As a result, the same person behaves differently in different settings. Polymorphism is the term for this. Polymorphism is one of the most significant elements of Object-Oriented Programming, and it can be performed at runtime or during compilation.
Golang is an Object-Oriented programming language that solely enables polymorphism through interfaces. The interfaces are implicitly implemented in Go, as we already know. When we construct an interface, and other kinds desire to implement it, those types utilize the interface's methods to implement it without knowing the type. A variable of an interface type can hold any value that implements the interface in an interface. In the Go programming language, this characteristic aids interface polymorphism. Let us understand with the help of an example:
Example
Interface is implemented by a variable of type interface that can hold any value. This property of interfaces is used to achieve polymorphism in Go.
Let's understand polymorphism in Go with the help of a program that calculates the total pages read by the student.
// Go program to illustrate the concept
// of polymorphism using interfaces
package main
import "fmt"
// Interface
type student interface {
pagecount() int
name() string
}
// Structure 1
type book1 struct {
totalpage_1 int
name_1 string
}
// Methods of employee interface are
// implemented by the team1 structure
func (b1 book1) pagecount() int {
return b1.totalpage_1
}
func (b1 book1) name() string {
return b1.name_1
}
// Structure 2
type book2 struct {
totalpage_2 int
name_2 string
}
// Methods of employee interface are
// implemented by the team2 structure
func (b2 book2) pagecount() int {
return b2.totalpage_2
}
func (b2 book2) name() string {
return b2.name_2
}
func finalpagecount(i []student) {
totalpagecount := 0
for _, ele := range i {
fmt.Printf("\nBook Name = %s\n ", ele.name())
fmt.Printf("Total number of pages %d\n ", ele.pagecount())
totalpagecount += ele.pagecount()
}
fmt.Printf("\nTotal pages read by "+"the student = %d", totalpagecount)
}
// Main function
func main() {
res1 := book1{totalpage_1: 180,
name_1: "The Great Gatsby"}
res2 := book2{totalpage_2: 256,
name_2: "Miss Pettigrew Lives for a Day"}
final := []student{res1, res2}
finalpagecount(final)
}
Output
Book Name = The Great Gatsby
Total number of pages 180
Book Name = Miss Pettigrew Lives for a Day
Total number of pages 256
Total pages read by the student = 436
Check out this article - Compile Time Polymorphism