# 要点

• interface关键字用来定义一个接口
• Go没有implements、extends等关键字
• 实现一个接口的方法就是直接定义接口中的方法
• 要实现多态，就要用指针或&object语法

# 示例

package main
import "fmt"
import "math"

type Shape interface {
area() float64
}

type Circle struct {
r float64
}

func (c *Circle) area() float64 {
return math.Pi * c.r * c.r
}

type Rectangle struct {
length, width float64
}

func (r *Rectangle) area() float64 {
return r.length * r.width;
}

func totalArea(shapes ... Shape) (total float64) {
total = 0
for _, shape := range shapes {
total += shape.area()
}

return
}

func main() {
circle := Circle{r:5}
ractangle := Rectangle{3, 4}

/*.\helloworld.go:40: cannot use circle (type Circle) as type Shape in argument to totalArea:
Circle does not implement Shape (area method has pointer receiver)
fmt.Println(totalArea(circle, ractangle)) // L40  */

fmt.Println(totalArea(&circle, &ractangle))
}


# 再论传值与传引用

## 基本类型

package main
import "fmt"

func foo(i int) {
fmt.Println(i)
}

func main() {
i := 5
foo(i)

/* cannot use &i (type *int) as type int in argument to foo
foo(&i)*/
}


## 结构体类型

package main
import "fmt"

type Data struct {
x int
}

func (data* Data) debug() {
fmt.Println(data.x)
}

func foo(data Data) {
data.x++
}

func bar(data *Data) {
data.x++
}

func main() {
data := Data{1}
data.debug()

// cannot use &data (type *Data) as type Data in argument to foo
//foo(&data)

foo(data)
data.debug()
}


## 特殊的interface

package main
import "fmt"

type Debugger interface {
debug()
}

type Data struct {
x int
}

func (data* Data) debug() {
fmt.Println(data.x)
}

func foo(debugger Debugger) {
debugger.debug()
}

func bar(debugger *Debugger) {
//debugger.debug undefined (type *Debugger is pointer to interface, not interface)
//debugger.debug()

(*debugger).debug()

//syntax error: unexpected >, expecting expression
//debugger->debug()
}

func main() {
data := Data{1}
data.debug()

/*cannot use data (type Data) as type Debugger in argument to foo:
Data does not implement Debugger (debug method has pointer receiver)*/
//foo(data)

foo(&data)

/*cannot use &data (type *Data) as type *Debugger in argument to bar:
*Debugger is pointer to interface, not interface*/
//bar(&data)

/*cannot use data (type Data) as type *Debugger in argument to bar:
*Debugger is pointer to interface, not interface*/
//bar(data)
}


# 约定俗成(???)

type I interface {...}


type Concreate struct {...}


func foo(i I) {...}


concreate := Concreate {...}
foo(&concreate)


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客