Skip to content

Instantly share code, notes, and snippets.

@nikzayn
Created June 16, 2022 11:37
Show Gist options
  • Save nikzayn/ea3f7fd48e3a9050424789f75816fb9d to your computer and use it in GitHub Desktop.
Save nikzayn/ea3f7fd48e3a9050424789f75816fb9d to your computer and use it in GitHub Desktop.
A brief example of how we can replicate the principle of polymorphism.
package polymorphism
// Developer struct
type Developer struct {
Fuel string
CanImplement bool
}
// Interface to add collection of methods
type Tasks interface {
Bugfix()
Improvement()
}
// Function using interface type in function parameter to get
// access to multiple methods via one interface similar
// to polymorphism
func SolveBugs(t Tasks) {
t.Bugfix()
t.Improvement()
}
// Method 1
func (d Developer) Bugfix() string {
if d.Fuel == "Coffee" {
return "Please solve the assigned bugs by EOD!"
}
}
// Method 2
func (d Developer) Improvement() string {
if d.CanImplement {
return "How many days it will take to implement this feature?"
}
}
func main() {
d := Developer{Fuel: "Coffee", CanImplement: false}
SolveBugs(d)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment