Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active February 20, 2022 07:47
Show Gist options
  • Save percybolmer/f279c3bf474bea2bef593695c1509be4 to your computer and use it in GitHub Desktop.
Save percybolmer/f279c3bf474bea2bef593695c1509be4 to your computer and use it in GitHub Desktop.
Benchmarking the type switched solution
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