Skip to content

Instantly share code, notes, and snippets.

@doorbash
Last active April 17, 2021 04:03
Show Gist options
  • Save doorbash/f955f9c766a427f10d93a3fe68529ed3 to your computer and use it in GitHub Desktop.
Save doorbash/f955f9c766a427f10d93a3fe68529ed3 to your computer and use it in GitHub Desktop.
go interface example
package main
import (
"fmt"
)
type Movable interface {
Translate(dx int, dy int)
}
type Position struct {
x int
y int
}
func (p *Position) Translate(dx int, dy int) {
p.x += dx
p.y += dy
}
func main() {
var m Movable = &Position{
x: 0,
y: 0,
}
m.Translate(100, 200)
fmt.Printf("(x, y) = (%d, %d)\n", m.(*Position).x, m.(*Position).y) // (x, y) = (100, 200)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment