Created
April 14, 2020 23:18
-
-
Save jrozner/75ba8144c0bda8bb2e0429373b9e72fb 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
package main | |
import "fmt" | |
func main() { | |
s1 := &FakeStruct1{} | |
runMethods(s1) | |
s2 := &FakeStruct2{} | |
runMethods(s2) | |
s3 := &FakeStruct3{} | |
runMethods(s3) | |
} | |
func runMethods(i SomeInterface) { | |
i.Method1() | |
i.Method2() | |
i.Method3() | |
i.Method4() | |
} | |
type SomeInterface interface { | |
Method1() | |
Method2() | |
Method3() | |
Method4() | |
} | |
type Nop struct{} | |
func (n *Nop) Method1() {} | |
func (n *Nop) Method2() {} | |
func (n *Nop) Method3() {} | |
func (n *Nop) Method4() {} | |
type FakeStruct1 struct { | |
Nop | |
} | |
func (f *FakeStruct1) Method1() { | |
fmt.Println("do something 1") | |
} | |
type FakeStruct2 struct { | |
Nop | |
} | |
func (f *FakeStruct1) Method2() { | |
fmt.Println("do something 2") | |
} | |
type FakeStruct3 struct { | |
Nop | |
} | |
func (f *FakeStruct3) Method3() { | |
fmt.Println("do something 3") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment