Created
July 27, 2020 09:13
-
-
Save maxwellgithinji/e9bc00861fded97cf667fb22591f462e 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
// type *T | |
// The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T (that is, it also contains the method set of 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