Last active
April 17, 2021 04:03
-
-
Save doorbash/f955f9c766a427f10d93a3fe68529ed3 to your computer and use it in GitHub Desktop.
go interface example
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 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