Created
July 27, 2020 09:11
-
-
Save maxwellgithinji/e2230e39d476714f6e611f471842fef0 to your computer and use it in GitHub Desktop.
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
// A type may have a method set associated with it. The method set of an interface type is its interface. | |
// type T | |
// The method set of any other type T consists of all methods declared with receiver type T. | |
package main | |
import ( | |
"fmt" | |
) | |
type Person struct { | |
first string | |
} | |
type secretAgent struct { | |
Person | |
ltk bool | |
} | |
func (p Person) speak() { | |
fmt.Println("from person", p.first) | |
} | |
func (sa secretAgent) speak() { | |
fmt.Println("from secret agent", sa.first) | |
} | |
type human interface { | |
speak() | |
} | |
func saySomething(h human) { | |
h.speak() | |
} | |
func main() { | |
p1 := Person{ | |
first: "Jorgen", | |
} | |
sa1 := secretAgent{ | |
Person: Person{ | |
first: "Ula Britta Frita Smirta", | |
}, | |
ltk: true, | |
} | |
saySomething(p1) | |
saySomething(sa1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment