Created
May 24, 2015 01:55
-
-
Save khoa-le/d2b486a2de72f22b0bea to your computer and use it in GitHub Desktop.
Implementing class(OOP style)
This file contains 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 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