go語言的interface是個很松散的概念杠河,類型不必明確聲明實現(xiàn)了接口,go語言通過內(nèi)部維護一個實現(xiàn)此接口方法的table來跟蹤诺核。實現(xiàn)一個接口的type也可以實現(xiàn)別的接口漂佩,也可以有自己的方法。interface編譯優(yōu)先級比較高豁翎。
慣例
向其他語言喜歡在接口前加個I 或者后面加個 able,go一般加個(e)r
定義
type Namer interface {
Method1(param_list) return_type
Method2(param_list) return_type
}
多態(tài)
go語言可以通過接口實現(xiàn)多態(tài)
package main
import (
"fmt"
)
type Shaper interface {
Area() float64
// Length() float32 //必須實現(xiàn)接口中定義的所有方法隅忿,才算實現(xiàn)了接口
}
type Square struct {
side float64
}
type Rectangle struct {
side float64
height float64
}
func (s *Square) Area() float64 {
return s.side * s.side
}
func (s *Square) Another() float64 {
return s.side * s.side
}
func (r *Rectangle) Area() float64 {
return r.side * r.height / 2
}
func main() {
sql := &Square{5}
rect := &Rectangle{10, 4}
//接口可以直接引用實現(xiàn)了此接口的類型的方法
//接口聲明,方式一
var areaIntf Shaper
areaIntf = sql
//接口聲明,方式二
areaIntf1 := Shaper(sql)
//接口聲明,方式三
areaIntf2 := sql
fmt.Println(areaIntf.Area())
fmt.Println(areaIntf1.Area())
fmt.Println(areaIntf2.Area())
//多態(tài)
areaIntf3 := Shaper(rect)
fmt.Println(areaIntf3.Area())
}
go也可以實現(xiàn)鴨式辯型,類似其他語言的轉(zhuǎn)型
package main
import (
"fmt"
)
type Shaper interface {
Area() float64
// Length() float32 //必須實現(xiàn)接口中定義的所有方法心剥,才算實現(xiàn)了接口
}
type Square struct {
side float64
}
type Rectangle struct {
side float64
height float64
}
func (s *Square) Area() float64 {
return s.side * s.side
}
func (s *Square) Another() float64 {
return s.side * s.side
}
func (r *Rectangle) Area() float64 {
return r.side * r.height / 2
}
type stockPosition struct {
ticker string
sharePrice float32
count float32
}
/* method to determine the value of a stock position */
func (s stockPosition) getValue() float32 {
return s.sharePrice * s.count
}
type car struct {
make string
model string
price float32
}
/* method to determine the value of a car */
func (c car) getValue() float32 {
return c.price
}
/* contract that defines different things that have value */
type valuable interface {
getValue() float32
}
/* anything that satisfies the “valuable” interface is accepted */
func showValue(asset valuable) {
fmt.Printf("Value of the asset is %f\n", asset.getValue())
}
func main() {
sql := &Square{5}
rect := &Rectangle{10, 4}
//接口可以直接引用實現(xiàn)了此接口的類型的方法
//接口聲明,方式一
var areaIntf Shaper
areaIntf = sql
//接口聲明,方式二
areaIntf1 := Shaper(sql)
//接口聲明,方式三
areaIntf2 := sql
fmt.Println(areaIntf.Area())
fmt.Println(areaIntf1.Area())
fmt.Println(areaIntf2.Area())
//多態(tài)
areaIntf3 := Shaper(rect)
fmt.Println(areaIntf3.Area())
//鴨式辯型,類似其他語言的轉(zhuǎn)型
var o valuable = stockPosition{"GOOG", 577.20, 4}
showValue(o)
o = car{"BMW", "M3", 66500}
showValue(o)
}
io包實現(xiàn)了以下接口
type Reader interface {
Read(p []byte) (n int, err error)
}
當var r io.Reader 時,可以這樣做
r = os.Stdin
r = bufio.NewReader(r)
r = new(bytes.Buffer)
f, _ := os.Open(“test.txt”)
r = bufio.NewReader(f)
嵌套
type ReadWrite interface {
Read(b Buffer) bool
Write(b Buffer) bool
}
type Lock interface {
Lock()
Unlock()
}
type File interface {
ReadWrite
Lock
Close()
}
類型斷言
//類型斷言
if t, ok := areaIntf.(*Square); ok {//必須是*Square 而不是Square
fmt.Printf("%T",t)//*main.Square
}
type switch
//type switch
switch areaIntf.(type) {
case *Square:
fmt.Println("square")
case *Rectangle:
fmt.Println("Rectangle")
default:
fmt.Println("not find")
}
接口轉(zhuǎn)換規(guī)則
receiver為value時背桐,可以通過值和指針類型調(diào)用,receiver位pointer時优烧,只能通過pointer來調(diào)用
type List []int
func (l List) Len() int {
return len(l)
}
可以
lst:=List
lst.Len()
lst:=new(List)
lst.Len()
func (l List) Append(val int) {
*l = append(*l, val)
}
只能
lst:=new(List)
lst.Append(2)
空接口
go中的空接口相當于其他語言的Object對象
反射
package main
import (
"fmt"
"reflect"
)
func main() {
var x float64 = 3.14
fmt.Println("Type:", reflect.TypeOf(x))
v:=reflect.ValueOf(x)
fmt.Println("Type:",v.Type())
fmt.Println("kind:",v.Kind())
fmt.Println("value:",v.Float())
fmt.Println(v.Interface())
fmt.Println(v.Interface().(float64))
fmt.Println(v.CanSet())
v=reflect.ValueOf(&x)
fmt.Println(v.CanSet())
v=v.Elem()
fmt.Println(v.CanSet())
v.SetFloat(3.141592)
fmt.Println(v.Interface())
fmt.Println(x)
}