Go 语言提供了另外一种数据类型即接口,它把所有的具有共性的方法定义在一起,任何其他类型只要实现了这些方法就是实现了这个接口

    1. // 定义接口
    2. type interface_name interface {
    3. method_name1 [return_type]
    4. method_name2 [return_type]
    5. method_name3 [return_type]
    6. ...
    7. method_namen [return_type]
    8. }
    9. // 实现接口方法
    10. func (struct_name_variable struct_name) method_name1() [return_type] {
    11. // 方法实现
    12. }
    13. // 定义结构体
    14. type struct_name struct {
    15. // variables
    16. }
    17. func (struct_name_variable struct_name) method_namen() [return_type] {
    18. // 方法实
    19. }
    1. package main
    2. import (
    3. "fmt"
    4. )
    5. type Phone interface {
    6. call() string
    7. say()
    8. }
    9. type NokiaPhone struct {
    10. name string
    11. }
    12. func (nokiaPhone NokiaPhone) say() {
    13. fmt.Println("Hello, I am Nokia")
    14. }
    15. func (nokiaPhone NokiaPhone) call() string {
    16. return "I am Nokia, I can call you!"
    17. }
    18. type IPhone struct {
    19. name string
    20. }
    21. func (iPhone IPhone) say(){
    22. fmt.Println("Hello, I am iPhone")
    23. }
    24. func (iPhone IPhone) call() string{
    25. return "I am iPhone, I can call you!"
    26. }
    27. func main() {
    28. var nokiaPhone NokiaPhone
    29. nokiaPhone = NokiaPhone{name:"nokiaPhone"}
    30. fmt.Println(nokiaPhone.name)
    31. nokiaPhone.call()
    32. nokiaPhone.say()
    33. i := IPhone{name:"张三"}
    34. var iphone Phone = i
    35. // iphone = new(IPhone)
    36. // iphone = &IPhone{name:"张三"} //注意iphone = new(IPhone)也可以写作前面这样的
    37. fmt.Println(i.name)
    38. iphone.call()
    39. iphone.say()
    40. i.say()
    41. }
    42. /*
    43. 在上面的例子中,我们定义了一个接口Phone,接口里面有一个方法call()。
    44. 然后我们在main函数里面定义了一个Phone类型变量,
    45. 并分别为之赋值为NokiaPhone和IPhone。然后调用call()方法,输出结果如下:
    46. I am Nokia, I can call you!
    47. I am iPhone, I can call you!
    48. */
    1. package main
    2. import "fmt"
    3. // Interface Men被Human,Student和Employee实现
    4. // 因为这三个类型都实现了这两个方法
    5. type Men interface {
    6. SayHi()
    7. Sing(lyrics string)
    8. }
    9. type Human struct {
    10. name string
    11. age int
    12. phone string
    13. }
    14. type Student struct {
    15. Human //匿名字段
    16. school string
    17. loan float32
    18. }
    19. type Employee struct {
    20. Human //匿名字段
    21. company string
    22. money float32
    23. }
    24. //Human实现SayHi方法
    25. func (h Human) SayHi() {
    26. fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone)
    27. }
    28. //Human实现Sing方法
    29. func (h Human) Sing(lyrics string) {
    30. fmt.Println("La la la la...", lyrics)
    31. }
    32. //Employee重载Human的SayHi方法
    33. func (e Employee) SayHi() {
    34. fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name,
    35. e.company, e.phone)
    36. }
    37. func main() {
    38. mike := Student{Human{"Mike", 25, "222-222-XXX"}, "MIT", 0.00}
    39. paul := Student{Human{"Paul", 26, "111-222-XXX"}, "Harvard", 100}
    40. sam := Employee{Human{"Sam", 36, "444-222-XXX"}, "Golang Inc.", 1000}
    41. Tom := Employee{Human{"Tom", 37, "222-444-XXX"}, "Things Ltd.", 5000}
    42. //定义Men类型的变量i
    43. var i Men
    44. //i能存储Student
    45. i = mike
    46. fmt.Println("This is Mike, a Student:")
    47. i.SayHi()
    48. i.Sing("November rain")
    49. //i也能存储Employee
    50. i = Tom
    51. fmt.Println("This is Tom, an Employee:")
    52. i.SayHi()
    53. i.Sing("Born to be wild")
    54. //定义了slice Men
    55. fmt.Println("Let's use a slice of Men and see what happens")
    56. x := make([]Men, 3)
    57. //这三个都是不同类型的元素,但是他们实现了interface同一个接口
    58. x[0], x[1], x[2] = paul, sam, mike
    59. for _, value := range x{
    60. value.SayHi()
    61. }
    62. }