Source file tour/methods/interface-values-with-nil.go

     1  // +build OMIT
     2  
     3  package main
     4  
     5  import "fmt"
     6  
     7  type I interface {
     8  	M()
     9  }
    10  
    11  type T struct {
    12  	S string
    13  }
    14  
    15  func (t *T) M() {
    16  	if t == nil {
    17  		fmt.Println("<nil>")
    18  		return
    19  	}
    20  	fmt.Println(t.S)
    21  }
    22  
    23  func main() {
    24  	var i I
    25  
    26  	var t *T
    27  	i = t
    28  	describe(i)
    29  	i.M()
    30  
    31  	i = &T{"hello"}
    32  	describe(i)
    33  	i.M()
    34  }
    35  
    36  func describe(i I) {
    37  	fmt.Printf("(%v, %T)\n", i, i)
    38  }
    39  

View as plain text