How are interfaces used in the code example 11.1 from the book The Way to Go?

huangapple go评论81阅读模式
英文:

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())
}

以上代码定义了一个接口Shaper,该接口包含一个Area方法。然后定义了一个结构体Square,该结构体有一个side字段,并实现了Area方法。

main函数中,创建了一个Square类型的实例sq1,并将其side设置为5。然后,通过将sq1赋值给areaIntf,将sq1转换为Shaper接口类型。最后,通过调用areaIntf.Area()打印出正方形的面积。

接口的作用是定义一组方法的集合,任何类型只要实现了接口中的所有方法,就被认为是实现了该接口。在这个例子中,Square结构体实现了Shaper接口中的Area方法,因此可以将Square类型的实例赋值给Shaper类型的变量,并调用Area方法。

希望这能帮助你理解接口在这段代码中的作用。如果还有其他问题,请随时提问。

英文:

I am learning Go and am trying to fully understand how to use interfaces in Go.

In the book The Way to Go, there is an example listing 11.1 (pages 264-265). I feel that I am definitely missing something in my understanding of it. The code runs fine, but I do not understand what effect (if any) the interface is having on the struct and method.

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())

}

答案1

得分: 1

在这个例子中,它没有影响。

接口允许不同的类型遵循一个共同的契约,从而可以创建通用的函数。

这是一个修改后的例子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())
}
英文:

In that example, it has no effect.

Interfaces allow different types to adhere to a common contract, allowing you to create generalized functions.

Here's a modified example on Play

Notice the printIt function, it can take any type that adheres to the Shaper interface.

Without interfaces, you would have had to make
printCircle and printRectangle methods, which doesn't really work as you add more and more types to your application over time.

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())
}

huangapple
  • 本文由 发表于 2015年6月4日 11:24:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/30634412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定