github.com/traefik/yaegi@v0.15.1/_test/interface2.go (about)

     1  package main
     2  
     3  import "fmt"
     4  
     5  type fii interface {
     6  	Hello()
     7  }
     8  
     9  type Boo struct {
    10  	Name string
    11  }
    12  
    13  type Bar struct{}
    14  
    15  func (b Bar) Hello() { fmt.Println("b:", b) }
    16  
    17  func (b Boo) Hello() {
    18  	fmt.Println("Hello", b)
    19  	fmt.Println(b.Name)
    20  }
    21  
    22  func inCall(foo fii) {
    23  	fmt.Println("inCall")
    24  	switch a := foo.(type) {
    25  	case Boo:
    26  		a.Hello()
    27  	default:
    28  		fmt.Println("a:", a)
    29  	}
    30  }
    31  
    32  func main() {
    33  	boo := Boo{"foo"}
    34  	inCall(boo)
    35  	inCall(Bar{})
    36  }
    37  
    38  // Output:
    39  // inCall
    40  // Hello {foo}
    41  // foo
    42  // inCall
    43  // a: {}