fork download
  1. package main
  2. import "fmt"
  3. import "reflect"
  4.  
  5. type Stringer interface {
  6. String() string
  7. }
  8.  
  9. type MyInt int
  10.  
  11. func (m MyInt) String() string {
  12. return fmt.Sprintf("I'm number %d!", m)
  13. }
  14.  
  15. func main() {
  16. defer func() { fmt.Println(recover()) }()
  17. var m = MyInt(10)
  18. m_type := reflect.TypeOf(m)
  19.  
  20. fmt.Println(m_type.Implements(reflect.TypeOf((*Stringer)(nil)).Elem()))
  21. fmt.Println(m_type.Implements(reflect.TypeOf(Stringer(nil)))) // This does not work.
  22. }
Success #stdin #stdout 0s 5660KB
stdin
Standard input is empty
stdout
true
reflect: nil type passed to Type.Implements