1. package main
    2. import (
    3. "fmt"
    4. "reflect"
    5. )
    6. type Dark interface {
    7. Run(string)
    8. Speak(string) string
    9. }
    10. type bird struct {
    11. }
    12. type chick struct {
    13. }
    14. func (c *chick) Run(msg string) {
    15. fmt.Printf(msg)
    16. }
    17. func (c *chick) Speak() string {
    18. return ""
    19. }
    20. func (b *bird) Run(msg string) {
    21. fmt.Print(msg)
    22. }
    23. func (b *bird) Speak(string) string {
    24. return ""
    25. }
    26. var (
    27. //判断结构体bird是否实现了Dark接口
    28. _ Dark = (*bird)(nil) //把nil转化成*bird类型 赋值后即丢弃
    29. _ Dark = (*chick)(nil)//没有实现所有方法,所有编译报错
    30. )
    31. func main() {
    32. var bird Dark = (*bird)(nil)
    33. var chick Dark = (*chick)(nil)
    34. fmt.Print(reflect.TypeOf(bird), reflect.TypeOf(chick))
    35. }