Created
June 16, 2022 11:37
-
-
Save nikzayn/ea3f7fd48e3a9050424789f75816fb9d to your computer and use it in GitHub Desktop.
A brief example of how we can replicate the principle of polymorphism.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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