go语言接口的使用

go语言接口定义 和 结构体的使用,代码如下:

package main

import "fmt"

type Phone interface {
	call()
}

type NokiaPhone struct {

}
type ApplePhone struct {

}


func (nokiaPhone NokiaPhone) call()  {
	fmt.Println("i am nokia , I can call you")
}


func (applePhone ApplePhone) call()  {
	fmt.Println("i am apple , I can call you")
}



func main()  {
	var phone Phone
	phone = new(NokiaPhone);

	phone.call()



	phone = new(ApplePhone);

	phone.call()

}

输出结果

i am nokia , I can call you
i am apple , I can call you