天天看點

Go中結構體和接口的定義

package main
import (
   "fmt"
)

//定義接口
type woman interface {//定義一個女人的接口,定義一個愛的方法
   love()
   makelove()
}
//定義一個結構體
type teacher struct {

   name string
   Age int
}
//實作接口
func (p *teacher)  love() {
   fmt.Println(p.Age,"gan")
}
//type human interface {
// //隻有聲明沒有實作,也沒有類型
// eat()
//}
//
//type Student struct {
// name string
//}
//
實作接口方法
//func (s *Student) eat() {
// fmt.Println(s.name + " eat")
//}

func main() {
   //s := Student{"yy"}
   tt :=teacher{"Alice",18}
   //(&s).eat()
   (&tt).love()
}