Skip to content

Instantly share code, notes, and snippets.

@TuranTimur
Last active August 29, 2015 14:23
Show Gist options
  • Save TuranTimur/82b899d65ca63e7cccef to your computer and use it in GitHub Desktop.
Save TuranTimur/82b899d65ca63e7cccef to your computer and use it in GitHub Desktop.
go-object-and-interface
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