Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active January 27, 2022 19:09
Show Gist options
  • Save percybolmer/7f1f73bb9d1b60019a063bfdbf9979ff to your computer and use it in GitHub Desktop.
Save percybolmer/7f1f73bb9d1b60019a063bfdbf9979ff to your computer and use it in GitHub Desktop.
This code is not working because type constraint
// Moveable is a interface that is used to handle many objects that are moveable
type Moveable interface {
Move(Subtractable)
}
// Person is a person, implements Moveable
type Person struct {
Name string
}
func (p Person) Move(meters Subtractable) {
fmt.Printf("%s moved %d meters\n", p.Name, meters)
}
// Car is a test struct for cars, implements Moveable
type Car struct {
Name string
}
func (c Car) Move(meters Subtractable) {
fmt.Printf("%s moved %d meters\n", c.Name, meters)
}
// Move is a generic function that takes in a Moveable and moves it
func Move[V Moveable, S Subtractable](v V, distance S, meters S) S {
v.Move(meters)
return Subtract(distance, meters)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment