1.接口会自动和拥有相同方法的结构体关联
/*
只要一个变量,含有接口类型中的所有方法,那么这个变量就实现这个接口。
如果一个变量含有了多个interface类型的方法,那么这个变量就实现了多个接口。
*/
type MyInterface interface{
GetName() string
GetAge() int
}
type StringInterface interface{
SumAgeSa() int
}
type CommonInterface interface{
MyInterface
StringInterface
GetGender() string
}
type Employee struct {
name string
age int
salary int
gender string
}
func (self *Employee) GetName() string {
return self.name
}
func (self *Employee) GetAge() int {
return self.age
}
func (self *Employee) SumAgeSa() int {
return self.age + self.salary
}
func (self *Employee) GetGender() string {
return self.gender
}
/*自动关联上了*/
func IntRun() {
// 空接口的使用,空接口类型的变量可以保存任何类型的值
// 空格口类型的变量非常类似于弱类型语言中的变量
var varEmptyInterface interface{}
fmt.Println("varEmptyInterface is of type %T\n", varEmptyInterface)
varEmptyInterface = 100
y := varEmptyInter