This file contains hidden or 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 Package3 | |
| myStruct := Package1.NewMyStruct() | |
| consumer := Package2.NewConsumer() | |
| consumer.DoSomething(myStruct) |
This file contains hidden or 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 consumer struct { } | |
| func NewConsumer() *consumer { | |
| return &consumer{} | |
| } | |
| func (*consumer) DoSomething(obj interfaceToConsume) { | |
| /* We can actually pass in a Package1.myStruct here! */ | |
| } |
This file contains hidden or 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 Package2 | |
| /* Implicit interface time! */ | |
| type interfaceToConsume interface { | |
| SomeFunc() int | |
| } |
This file contains hidden or 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 Package1 | |
| type myStruct struct { } | |
| func (*myStruct) SomeFunc() int { return 42 } | |
| func (*myStruct) SomeOtherFunc() string { return "42" } | |
| /* Factory */ | |
| func NewMyStruct() *myStruct { | |
| return &myStruct{} |
This file contains hidden or 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 MyInterface interface { | |
| MethodMustBeImplemented() | |
| } | |
| type MyStruct struct { } | |
| func (*MyStruct) MethodMustBeImplemented() { } // Implicit |
This file contains hidden or 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
| interface MyInterface { } | |
| class MyClass implements MyInterface { } // Explicit |
This file contains hidden or 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
| func (s MyStruct) StructFunc() { | |
| if s.child == (ChildStruct{}) { | |
| return | |
| } | |
| fmt.Println(s.child.num) | |
| } |
This file contains hidden or 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 MyStruct struct { | |
| /* Note how s.child is not a pointer any more */ | |
| child ChildStruct | |
| } | |
| func (s *MyStruct) StructFunc() { | |
| if s.child != nil { | |
| fmt.Println(s.child.num) | |
| } | |
| } |
This file contains hidden or 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 MyStruct struct { | |
| child *ChildStruct | |
| } | |
| func (s *MyStruct) StructFunc() { | |
| if s.child != nil { | |
| fmt.Println(s.child.num) | |
| } | |
| } |
This file contains hidden or 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 MyStruct struct { | |
| child *ChildStruct | |
| } | |
| func (s MyStruct) StructFunc() { | |
| /* Here's where things go wrong */ | |
| fmt.Println(s.child.num) | |
| } | |
| type ChildStruct struct { |