《The Way to Go》一书中的代码示例 11.1 中的接口是如何使用的?
How are interfaces used in the code example 11.1 from the book The Way to Go?
我正在学习 Go 并试图完全理解如何在 Go 中使用接口。
在 The Way to Go 一书中,有一个示例清单 11.1(第 264-265 页)。我觉得我对它的理解肯定遗漏了一些东西。代码运行良好,但我不明白接口对结构和方法有什么影响(如果有的话)。
package main
import "fmt"
type Shaper interface {
Area() float32
}
type Square struct {
side float32
}
func (sq *Square) Area() float32 {
return sq.side * sq.side
}
func main() {
sq1 := new(Square)
sq1.side = 5
// var areaIntf Shaper
// areaIntf = sq1
// shorter, without separate declaration:
// areaIntf := Shaper(sq1)
// or even:
areaIntf := sq1
fmt.Printf("The square has area: %f\n", areaIntf.Area())
}
在那个例子中,它没有效果。
接口允许不同的类型遵守共同的契约,允许您创建通用函数。
这是 Play
上的修改示例
注意 printIt 函数,它可以采用任何符合 Shaper 接口的类型。
如果没有接口,您将不得不
printCircle 和 printRectangle 方法,当您随着时间的推移向您的应用程序中添加越来越多的类型时,它们并没有真正起作用。
package main
import (
"fmt"
"math"
)
type Shaper interface {
Area() float32
}
type Square struct {
side float32
}
func (sq *Square) Area() float32 {
return sq.side * sq.side
}
type Circle struct {
radius float32
}
func (c *Circle) Area() float32 {
return math.Pi * c.radius * c.radius
}
func main() {
sq1 := new(Square)
sq1.side = 5
circ1 := new(Circle)
circ1.radius = 5
var areaIntf Shaper
areaIntf = sq1
// areaIntf = sq1
// shorter, without separate declaration:
// areaIntf := Shaper(sq1)
// or even:
fmt.Printf("The square has area: %f\n", areaIntf.Area())
areaIntf = circ1
fmt.Printf("The circle has area: %f\n", areaIntf.Area())
// Here is where interfaces are actually interesting
printIt(sq1)
printIt(circ1)
}
func printIt(s Shaper) {
fmt.Printf("Area of this thing is: %f\n", s.Area())
}
我正在学习 Go 并试图完全理解如何在 Go 中使用接口。
在 The Way to Go 一书中,有一个示例清单 11.1(第 264-265 页)。我觉得我对它的理解肯定遗漏了一些东西。代码运行良好,但我不明白接口对结构和方法有什么影响(如果有的话)。
package main
import "fmt"
type Shaper interface {
Area() float32
}
type Square struct {
side float32
}
func (sq *Square) Area() float32 {
return sq.side * sq.side
}
func main() {
sq1 := new(Square)
sq1.side = 5
// var areaIntf Shaper
// areaIntf = sq1
// shorter, without separate declaration:
// areaIntf := Shaper(sq1)
// or even:
areaIntf := sq1
fmt.Printf("The square has area: %f\n", areaIntf.Area())
}
在那个例子中,它没有效果。
接口允许不同的类型遵守共同的契约,允许您创建通用函数。
这是 Play
上的修改示例注意 printIt 函数,它可以采用任何符合 Shaper 接口的类型。
如果没有接口,您将不得不 printCircle 和 printRectangle 方法,当您随着时间的推移向您的应用程序中添加越来越多的类型时,它们并没有真正起作用。
package main
import (
"fmt"
"math"
)
type Shaper interface {
Area() float32
}
type Square struct {
side float32
}
func (sq *Square) Area() float32 {
return sq.side * sq.side
}
type Circle struct {
radius float32
}
func (c *Circle) Area() float32 {
return math.Pi * c.radius * c.radius
}
func main() {
sq1 := new(Square)
sq1.side = 5
circ1 := new(Circle)
circ1.radius = 5
var areaIntf Shaper
areaIntf = sq1
// areaIntf = sq1
// shorter, without separate declaration:
// areaIntf := Shaper(sq1)
// or even:
fmt.Printf("The square has area: %f\n", areaIntf.Area())
areaIntf = circ1
fmt.Printf("The circle has area: %f\n", areaIntf.Area())
// Here is where interfaces are actually interesting
printIt(sq1)
printIt(circ1)
}
func printIt(s Shaper) {
fmt.Printf("Area of this thing is: %f\n", s.Area())
}