Last active
February 20, 2022 07:47
-
-
Save percybolmer/f279c3bf474bea2bef593695c1509be4 to your computer and use it in GitHub Desktop.
Benchmarking the type switched solution
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 benchmarking | |
// Below is the Type casting based Solution | |
// | |
type CarRegular struct { | |
Name string | |
DistanceMoved int | |
} | |
type PersonRegular struct { | |
Name string | |
DistanceMoved float32 | |
} | |
func (p *PersonRegular) Move(meters float32) { | |
p.DistanceMoved += meters | |
} | |
func (c *CarRegular) Move(meters int) { | |
c.DistanceMoved += meters | |
} | |
func MoveRegular(v interface{}, distance float32) { | |
switch v.(type) { | |
case *PersonRegular: | |
v.(*PersonRegular).Move(distance) | |
case *CarRegular: | |
v.(*CarRegular).Move(int(distance)) | |
default: | |
// Handle Unsupported types, not needed by Generic solution as Compiler does this for you | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment