Skip to content

Instantly share code, notes, and snippets.

@fengyfei
Last active September 24, 2019 03:08
Show Gist options
  • Save fengyfei/85042bff520045777d9d414d1af6d4c2 to your computer and use it in GitHub Desktop.
Save fengyfei/85042bff520045777d9d414d1af6d4c2 to your computer and use it in GitHub Desktop.
[Go][Struct][Selector] Collection
package main
import (
"fmt"
)
type Sample interface {
Any()
Some()
}
type PartI struct {
}
func (p *PartI) Any() {
fmt.Println("PartI::Any()")
}
type PartII struct {
}
func (p *PartII) Some() {
fmt.Println("PartII::Some()")
}
type PartIII struct {
}
func (p *PartIII) Any() {
fmt.Println("PartIII::Any()")
}
func (p *PartIII) Some() {
fmt.Println("PartIII::Some()")
}
type Collection struct {
PartI
// PartIII
PartII
}
func main() {
c := &Collection{
PartI{},
// PartIII{},
PartII{},
}
c.Any()
c.Some()
}
/*
package main
import (
"fmt"
)
type Sample interface {
Any()
Some()
}
type PartI struct {
}
func (p *PartI) Any() {
fmt.Println("PartI::Any()")
}
type PartII struct {
}
func (p *PartII) Some() {
fmt.Println("PartII::Some()")
}
type PartIII struct {
}
func (p *PartIII) Any() {
fmt.Println("PartIII::Any()")
}
func (p *PartIII) Some() {
fmt.Println("PartIII::Some()")
}
type Collection struct {
PartI
p PartIII
PartII
}
func main() {
c := &Collection{
PartI{},
PartIII{},
PartII{},
}
c.Any()
c.Some()
c.p.Any()
c.p.Some()
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment