Skip to content

Instantly share code, notes, and snippets.

@khoa-le
Created May 24, 2015 01:55
Show Gist options
  • Save khoa-le/d2b486a2de72f22b0bea to your computer and use it in GitHub Desktop.
Save khoa-le/d2b486a2de72f22b0bea to your computer and use it in GitHub Desktop.
Implementing class(OOP style)
package main
import (
"fmt"
)
type SayHelloIntFace interface {
SayHello()
}
type Person struct{}
// this method is tied to Person class
func (person Person) SayHello() {
fmt.Printf("Hello!")
}
type Dog struct{}
// this method is tied to Dog class
func (dog Dog) SayHello() {
fmt.Printf("woof! woof!")
}
func greeting(i SayHelloIntFace) {
i.SayHello()
}
func main() {
// instantiate objects
person := Person{}
dog := Dog{}
var i SayHelloIntFace
fmt.Println("\nPerson : ")
i = person
greeting(i)
fmt.Println("\n\nDog : ")
i = dog
greeting(i)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment