从 Go 编译器获取“未定义:距离”错误

我是一位经验丰富的“老派”程序员,但也是 Go 的初学者。我正在阅读“CreateSpace:Go 编程简介”一书。在第。111,第9章的第三章习题,任务是给用户自定义的Shape接口添加一个新的方法。界面已经在本章的过程中建立起来了,这是我目前所拥有的:


package main


import (

    "fmt"

    "math"

)


type Shape interface {

    area() float64

    perimeter() float64

}


type Distance struct {

    x1, y1, x2, y2 float64

}


func (d *Distance) distance() float64 {

    a := d.x2 - d.x1

    b := d.y2 - d.y1

    return math.Sqrt(a*a + b*b)

}


type Rectangle struct {

    x1, y1, x2, y2 float64

}


func (r *Rectangle) area() float64 {

    l := distance(r.x1, r.y1, r.x2, r.y1)

    w := distance(r.x1, r.y1, r.x1, r.y2)

    return l * w

}


type Circle struct {

    x, y, r float64

}


func (c *Circle) area() float64 {

    return math.Pi * c.r * c.r

}


type Perimeter struct {

    x1, y1, x2, y2 float64

}


func (p *Perimeter) perimeter() float64 {

    s1 := distance(p.x1, p.y1, p.x1, p.y2)

    s2 := distance(p.x1, p.y2, p.x2, p.y2)

    s3 := distance(p.x2, p.y2, p.x2, p.y1)

    s4 := distance(p.x2, p.y1, p.x1, p.y1)

    return s1 + s2 + s3 + s4

}


func main() {

    d := new(Distance)

    d.x2, d.y2, d.x1, d.y1 = 0, 0, 10, 10


    p := new(Perimeter)

    p.x1, p.y1 = 0, 0

    p.x2, p.y2 = 10, 10

    fmt.Println(p.perimeter())


    r := new(Rectangle)

    r.x1, r.y1 = 0, 0

    r.x2, r.y2 = 10, 10

    fmt.Println(r.area())


    c := Circle{0, 0, 5}

    fmt.Println(c.area())


}

问题是我收到以下错误(来自编译器?):


user@pc /c/Go/src/golang-book/chapter9/chapterProblems

$ go run interface.go

# command-line-arguments

.\interface.go:25: undefined: distance

.\interface.go:26: undefined: distance

.\interface.go:42: undefined: distance

.\interface.go:43: undefined: distance

.\interface.go:44: undefined: distance

.\interface.go:45: undefined: distance

我花了很多时间重新阅读章节文本并仔细思考这个“未定义:距离”错误可能意味着什么,但到目前为止无济于事。正如你所看到的,我已经定义了一个“距离结构”,创建了一个名为 的 new() 实例,d用.运算符初始化了它的字段,并创建了一个distance()func,但是,很明显,我不是在探索一些相关的部分) 的信息。


一只甜甜圈
浏览 184回答 3
3回答

慕斯王

您没有名为distance. 这是一个类型的方法*Distance。您需要先创建一个*Distance,然后调用该方法。d := &Distance{r.x1, r.y1, r.x2, r.y1}l := d.distance()我建议从Effective Go开始。对于“经验丰富的程序员”来说,这是对语言的很好的介绍。

喵喔喔

您在此处定义的函数:func (d *Distance) distance() float64 {    a := d.x2 - d.x1    b := d.y2 - d.y1    return math.Sqrt(a*a + b*b)}是距离对象的一个方法。看起来您正在尝试在此处创建一个新的 Distance 实例:func (r *Rectangle) area() float64 {    l := distance(r.x1, r.y1, r.x2, r.y1)    w := distance(r.x1, r.y1, r.x1, r.y2)    return l.distance() * w.distance()}但你实际上在做的是试图调用一个名为distance.你要func (r *Rectangle) area() float64 {    l := &Distance{r.x1, r.y1, r.x2, r.y1}    w := &Distance{r.x1, r.y1, r.x1, r.y2}    return l.distance() * w.distance()}

慕桂英546537

在我的“头脑正确”之后(必须修复几个自己造成的语法错误),以下工作:<pre><code>package mainimport ("fmt"; "math")type Shape interface {&nbsp; &nbsp; area() float64&nbsp; &nbsp; perimeter() float64}type Distance struct {&nbsp; &nbsp; x1, y1, x2, y2 float64}func distance(x1, y1, x2, y2 float64) float64 {&nbsp; &nbsp; a := x2 - x1&nbsp; &nbsp; b := y2 - y1&nbsp; &nbsp; return math.Sqrt(a*a + b*b)}type Rectangle struct {&nbsp; &nbsp; x1, y1, x2, y2 float64}func (r *Rectangle) area() float64 {&nbsp; &nbsp; l&nbsp; := distance(r.x1, r.y1, r.x2, r.y1)&nbsp; &nbsp; w := distance(r.x1, r.y1, r.x1, r.y2)&nbsp; &nbsp; return l * w}type Circle struct {&nbsp; &nbsp; x, y, r float64}func (c *Circle) area() float64 {&nbsp; &nbsp; return math.Pi * c.r*c.r}type Perimeter struct {&nbsp; &nbsp; x1, y1, x2, y2 float64}func (p *Perimeter) perimeter() float64 {&nbsp; &nbsp; s1 := distance(p.x1, p.y1, p.x1, p.y2)&nbsp; &nbsp; s2 := distance(p.x1, p.y2, p.x2, p.y2)&nbsp; &nbsp; s3 := distance(p.x2, p.y2, p.x2, p.y1)&nbsp; &nbsp; s4 := distance(p.x2, p.y1, p.x1, p.y1)&nbsp; &nbsp; return s1 + s2 + s3 + s4}func main() {&nbsp; &nbsp; d := new(Distance)&nbsp; &nbsp; d.x1, d.y1, d.x2, d.y2 = 0, 0, 10, 10&nbsp; &nbsp; p := new(Perimeter)&nbsp; &nbsp; p.x1, p.y1, p.x2, p.y2 = 0, 0, 10, 10&nbsp; &nbsp; fmt.Println(p.perimeter())&nbsp; &nbsp; r := new(Rectangle)&nbsp; &nbsp; r.x1, r.y1 = 0, 0&nbsp; &nbsp; r.x2, r.y2 = 10, 10&nbsp; &nbsp; fmt.Println(r.area())&nbsp; &nbsp; c := new(Circle)&nbsp; &nbsp; c.x, c.y, c.r = 0, 0, 5&nbsp; &nbsp; fmt.Println(c.area())}<pre><code>这是结果输出:<pre><code>David Bailey@DAVIDBAILEY-PC /c/Go/src/golang-book/chapter9/chapterProblems$ go run interface.go4010078.53981633974483David Bailey@DAVIDBAILEY-PC /c/Go/src/golang-book/chapter9/chapterProblems$<pre><code>再次谢谢你。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go