Last active
August 29, 2015 14:23
-
-
Save TuranTimur/82b899d65ca63e7cccef to your computer and use it in GitHub Desktop.
go-object-and-interface
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 Duck interface { | |
Quak() string | |
} | |
type MyDuck struct { // create a type | |
name string | |
} | |
// this is where predefined type and function is merged | |
func (s MyDuck) Quak() string{ | |
return s.name | |
} | |
func main() { | |
n := MyDuck{name:"duggy"} // create type that is binded to a function | |
fmt.Println("duck name is: ", n.name) // call type's data | |
fmt.Println("duck is saying his name: " ,n.Quak()) // call type's function | |
s := Duck(MyDuck{name:"newduggy"}) // inject type that has data and function | |
fmt.Println("new duck is saying his name", s.Quak()) // things are inherited | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment