Polymorphism in GoLang Last Updated : 15 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Polymorphism is the ability of a message to be displayed in more than one form. Polymorphism is considered one of the important features of Object-Oriented Programming and can be achieved during either runtime or compile time. Golang is a light-object-oriented language and supports polymorphism through interfaces only. Let us first understand the interfaces by the following exampleExample 1: Go // Golang program to illustrate the // concept of interfaces package main import ( "fmt" ) // defining an interface type Figure interface{ Area() float64 } // declaring a struct type Rectangle struct{ // declaring struct variables length float64 width float64 } // declaring a struct type Square struct{ // declaring struct variable side float64 } // function to calculate // area of a rectangle func (rect Rectangle) Area() float64{ // Area of rectangle = l * b area := rect.length * rect.width return area } // function to calculate // area of a square func (sq Square) Area() float64{ // Area of square = a * a area := sq.side * sq.side return area } // main function func main() { // declaring a rectangle instance rectangle := Rectangle{ length: 10.5, width: 12.25, } // declaring a square instance square := Square{ side: 15.0, } // The Figure interface can hold rectangle and square type as they both implements the interface var f1 Figure = rectangle var f2 Figure = square // printing the calculated result fmt.Printf("Area of rectangle: %.3f unit sq.\n", f1.Area()) fmt.Printf("Area of square: %.3f unit sq.\n", f2.Area()) } Output: Area of rectangle: 128.625 unit sq. Area of square: 225.000 unit sq. Objects of different types are treated in a consistent way, as long as they stick to a single interface, which is the essence of polymorphism. Variable declared in an interface are of interface type. They can take whichever value implements the interface which helps interfaces to achieve polymorphism in the Golang. The following example explains the concept of polymorphism:Example 2: Go // Golang program to illustrate the // concept of polymorphism package main import ( "fmt" ) // defining an interface type Reading interface{ // declaring interface method reading_time() int } // declaring a struct type Book struct{ // declaring struct variables name string page_count int } // declaring a struct type Newspaper struct{ // declaring struct variables name string page_count int } // declaring a struct type Magazine struct{ // declaring struct variables name string page_count int } // function to calculate reading // time for book func (book Book) reading_time() int { // taking average speed // of 10 mins per page read_time := 10 * book.page_count return read_time } // function to calculate reading // time for newspaper func (newspaper Newspaper) reading_time() int { // taking average speed // of 30 mins per page read_time := 30 * newspaper.page_count return read_time } // function to calculate reading // time for magazine func (magazine Magazine) reading_time() int { // taking average speed // of 5 mins per page read_time := 5 * magazine.page_count return read_time } // function to calculate reading time func calcReadingTime(ReadingTime []Reading) int { totalTime := 0 // looping through elements // of the Reading array for _, t := range ReadingTime { // run time polymorphism, call to // method depends on object being // referred at run time totalTime += t.reading_time() } return totalTime } // main function func main() { // declaring a book instance book1 := Book{ name: "Goosebumps", page_count: 150, } // declaring a newspaper instance newspaper1 := Newspaper{ name: "TOI", page_count: 12, } // declaring a magazine instance magazine1 := Magazine{ name: "Forbes", page_count: 40, } // array of type Reading interface ReadingTime := []Reading{book1, newspaper1, magazine1} // total reading time calculated totalTime := calcReadingTime(ReadingTime) // Printing total time for reading fmt.Printf("Total Time is %d minutes.\n", totalTime) } Output: Total Time is 2060 minutes. Comment More infoAdvertise with us Next Article Golang | Polymorphism Using Interfaces V vanigupta20024 Follow Improve Article Tags : Programming Language Go Language Golang-Interfaces Golang-OOPs Similar Reads Golang | Polymorphism Using Interfaces The word polymorphism means having many forms. Or in other words, we can define polymorphism as the ability of a message to be displayed in more than one form. Or in technical term polymorphism means same method name (but different signatures) being uses for different types. For example, a woman at 4 min read Golang | Polymorphism Using Interfaces The word polymorphism means having many forms. Or in other words, we can define polymorphism as the ability of a message to be displayed in more than one form. Or in technical term polymorphism means same method name (but different signatures) being uses for different types. For example, a woman at 4 min read Golang | Polymorphism Using Interfaces The word polymorphism means having many forms. Or in other words, we can define polymorphism as the ability of a message to be displayed in more than one form. Or in technical term polymorphism means same method name (but different signatures) being uses for different types. For example, a woman at 4 min read Methods in Golang Go methods are like functions but with a key difference: they have a receiver argument, which allows access to the receiver's properties. The receiver can be a struct or non-struct type, but both must be in the same package. Methods cannot be created for types defined in other packages, including bu 3 min read Templates in GoLang Template in Golang is a robust feature to create dynamic content or show customized output to the user. Golang has two packages with templates: text/template html/template There are mainly 3 parts of a template which are as follows: 1. Actions They are data evaluations, control structures like loops 4 min read Import in GoLang Pre-requisite learning: Installing Go on Windows / Installing Go on MacOSÂ Technically defining, a package is essentially a container of source code for some specific purpose. This means that the package is a capsule that holds multiple elements of drug/medicine binding them all together and protect 9 min read Like