1. 什么是接口
- 概念:接口定義了一組方法箍鼓,但是不包含這些方法的具體實(shí)現(xiàn);它們是抽象的呵曹,接口不能包含變量款咖。
接口的定義格式
-
type Namer interface {
method1() return_type
method2() return_type
}
2. 一個(gè)接口的栗子
package main
import "fmt"
type Message interface {
Send()
}
type user struct {
name string
score int
}
func (u *user) Send() {
fmt.Println("hi", u.name, "this is your ", u.score)
}
func sendMessage(msg Message) {
msg.Send()
}
func main() {
u := &user{name: "roty", score: 44}
sendMessage(&u)
}
ps:暫時(shí)先寫這么多