概述
在Go語言中,接口(interface)是一個自定義類型,接口類型具體描述了一系列方法的集合。
接口類型是一種抽象的類型,它不會暴露出它所代表的對象的內部值的結構和這個對象支持的基礎操作的集合,它們只會展示出它們自己的方法。因此接口類型不能將其實例化。
Go通過接口實現了鴨子類型(duck-typing):“當看到一只鳥走起來像鴨子、游泳起來像鴨子、叫起來也像鴨子,那么這只鳥就可以被稱為鴨子”。我們并不關心對象是什么類型,到底是不是鴨子,只關心行為。
接口的使用
接口定義
type Humaner interface {
SayHi()
}
- 接口命名習慣以 er 結尾
- 接口只有方法聲明,沒有實現,沒有數據字段
- 接口可以匿名嵌入其它接口,或嵌入到結構中
接口實現
接口是用來定義行為的類型。這些被定義的行為不由接口直接實現,而是通過方法由用戶定義的類型實現,一個實現了這些方法的具體類型是這個接口類型的實例。
如果用戶定義的類型實現了某個接口類型聲明的一組方法,那么這個用戶定義的類型的值就可以賦給這個接口類型的值。這個賦值會把用戶定義的類型的值存入接口類型的值。
type Humaner interface {
SayHi()
}
type Student struct { //學生
name string
score float64
}
//Student實現SayHi()方法
func (s *Student) SayHi() {
fmt.Printf("Student[%s, %f] say hi!!\n", s.name, s.score)
}
type Teacher struct { //老師
name string
group string
}
//Teacher實現SayHi()方法
func (t *Teacher) SayHi() {
fmt.Printf("Teacher[%s, %s] say hi!!\n", t.name, t.group)
}
type MyStr string
//MyStr實現SayHi()方法
func (str MyStr) SayHi() {
fmt.Printf("MyStr[%s] say hi!!\n", str)
}
//普通函數,參數為Humaner類型的變量i
func WhoSayHi(i Humaner) {
i.SayHi()
}
func main() {
s := &Student{"mike", 88.88}
t := &Teacher{"yoyo", "Go語言"}
var tmp MyStr = "測試"
s.SayHi() //Student[mike, 88.880000] say hi!!
t.SayHi() //Teacher[yoyo, Go語言] say hi!!
tmp.SayHi() //MyStr[測試] say hi!!
//多態,調用同一接口,不同表現
WhoSayHi(s) //Student[mike, 88.880000] say hi!!
WhoSayHi(t) //Teacher[yoyo, Go語言] say hi!!
WhoSayHi(tmp) //MyStr[測試] say hi!!
x := make([]Humaner, 3)
//這三個都是不同類型的元素,但是他們實現了interface同一個接口
x[0], x[1], x[2] = s, t, tmp
for _, value := range x {
value.SayHi()
}
/*
Student[mike, 88.880000] say hi!!
Teacher[yoyo, Go語言] say hi!!
MyStr[測試] say hi!!
*/
}
通過上面的代碼,你會發現接口就是一組抽象方法的集合,它必須由其他非接口類型實現,而不能自我實現。
接口組合
接口嵌入
如果一個interface1作為interface2的一個嵌入字段,那么interface2隱式的包含了interface1里面的方法。
type Humaner interface {
SayHi()
}
type Personer interface {
Humaner //這里想寫了SayHi()一樣
Sing(lyrics string)
}
type Student struct { //學生
name string
score float64
}
//Student實現SayHi()方法
func (s *Student) SayHi() {
fmt.Printf("Student[%s, %f] say hi!!\n", s.name, s.score)
}
//Student實現Sing()方法
func (s *Student) Sing(lyrics string) {
fmt.Printf("Student sing[%s]!!\n", lyrics)
}
func main() {
s := &Student{"mike", 88.88}
var i2 Personer
i2 = s
i2.SayHi() //Student[mike, 88.880000] say hi!!
i2.Sing("學生哥") //Student sing[學生哥]!!
}
接口轉換
超集接?對象可轉換為?集接?,反之出錯:
type Humaner interface {
SayHi()
}
type Personer interface {
Humaner //這里像寫了SayHi()一樣
Sing(lyrics string)
}
type Student struct { //學生
name string
score float64
}
//Student實現SayHi()方法
func (s *Student) SayHi() {
fmt.Printf("Student[%s, %f] say hi!!\n", s.name, s.score)
}
//Student實現Sing()方法
func (s *Student) Sing(lyrics string) {
fmt.Printf("Student sing[%s]!!\n", lyrics)
}
func main() {
//var i1 Humaner = &Student{"mike", 88.88}
//var i2 Personer = i1 //err
//Personer為超集,Humaner為子集
var i1 Personer = &Student{"mike", 88.88}
var i2 Humaner = i1
i2.SayHi() //Student[mike, 88.880000] say hi!!
}
空接口
空接口(interface{})不包含任何的方法,正因為如此,所有的類型都實現了空接口,因此空接口可以存儲任意類型的數值。它有點類似于C語言的void *類型。
var v1 interface{} = 1 // 將int類型賦值給interface{}
var v2 interface{} = "abc" // 將string類型賦值給interface{}
var v3 interface{} = &v2 // 將*interface{}類型賦值給interface{}
var v4 interface{} = struct{ X int }{1}
var v5 interface{} = &struct{ X int }{1}
當函數可以接受任意的對象實例時,我們會將其聲明為interface{},最典型的例子是標準庫fmt中PrintXXX系列的函數,例如:
func Printf(fmt string, args ...interface{})
func Println(args ...interface{})
類型查詢
我們知道interface的變量里面可以存儲任意類型的數值(該類型實現了interface)。那么我們怎么反向知道這個變量里面實際保存了的是哪個類型的對象呢?目前常用的有兩種方法:
- comma-ok斷言
- switch測試
comma-ok斷言
Go語言里面有一個語法,可以直接判斷是否是該類型的變量: value, ok = element.(T),這里value就是變量的值,ok是一個bool類型,element是interface變量,T是斷言的類型。
如果element里面確實存儲了T類型的數值,那么ok返回true,否則返回false。
type Element interface{}
type Person struct {
name string
age int
}
func main() {
list := make([]Element, 3)
list[0] = 1 // an int
list[1] = "Hello" // a string
list[2] = Person{"mike", 18}
for index, element := range list {
if value, ok := element.(int); ok {
fmt.Printf("list[%d] is an int and its value is %d\n", index, value)
} else if value, ok := element.(string); ok {
fmt.Printf("list[%d] is a string and its value is %s\n", index, value)
} else if value, ok := element.(Person); ok {
fmt.Printf("list[%d] is a Person and its value is [%s, %d]\n", index, value.name, value.age)
} else {
fmt.Printf("list[%d] is of a different type\n", index)
}
}
/* 打印結果:
list[0] is an int and its value is 1
list[1] is a string and its value is Hello
list[2] is a Person and its value is [mike, 18]
*/
}
switch測試
type Element interface{}
type Person struct {
name string
age int
}
func main() {
list := make([]Element, 3)
list[0] = 1 //an int
list[1] = "Hello" //a string
list[2] = Person{"mike", 18}
for index, element := range list {
switch value := element.(type) {
case int:
fmt.Printf("list[%d] is an int and its value is %d\n", index, value)
case string:
fmt.Printf("list[%d] is a string and its value is %s\n", index, value)
case Person:
fmt.Printf("list[%d] is a Person and its value is [%s, %d]\n", index, value.name, value.age)
default:
fmt.Println("list[%d] is of a different type", index)
}
}
}