我是一名经验丰富的“老派”程序员,但是Go的初学者。我正在通过“CreateSpace:一个简介 在Go“book中编程。在第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
我花了很多时间做我的“尽职调查”,重新阅读章节文本并仔细思考这是什么
“undefined:distance”错误可能意味着,但到目前为止无济于事。
如您所见,我定义了一个“Distance struct”,创建了一个new()实例
它调用了d
,用.
运算符初始化了它的字段,并创建了一个distance()
函数,但很明显,我并没有找到一些相关的信息。
答案 0 :(得分:2)
您没有名为distance
的功能。它是*Distance
类型的方法。您需要先创建*Distance
,然后调用该方法。
d := &Distance{r.x1, r.y1, r.x2, r.y1}
l := d.distance()
我建议从Effective Go开始。对于经验丰富的程序员来说,这是对语言的非常好的介绍"。
答案 1 :(得分:2)
您在此处定义的功能:
func (d *Distance) distance() float64 {
a := d.x2 - d.x1
b := d.y2 - d.y1
return math.Sqrt(a*a + b*b)
}
是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()
}
但你实际上正在尝试调用名为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()
}
答案 2 :(得分:0)
感谢@ Ainar-G和@Momer!在我的“头脑正确”(必须修复一些更多自我造成的语法错误)后,以下工作:
<pre><code>
package main
import ("fmt"; "math")
type Shape interface {
area() float64
perimeter() float64
}
type Distance struct {
x1, y1, x2, y2 float64
}
func distance(x1, y1, x2, y2 float64) float64 {
a := x2 - x1
b := y2 - 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.x1, d.y1, d.x2, d.y2 = 0, 0, 10, 10
p := new(Perimeter)
p.x1, p.y1, p.x2, p.y2 = 0, 0, 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 := new(Circle)
c.x, c.y, c.r = 0, 0, 5
fmt.Println(c.area())
}
<pre><code>
以下是结果输出:
<pre><code>
David Bailey@DAVIDBAILEY-PC /c/Go/src/golang-book/chapter9/chapterProblems
$ go run interface.go
40
100
78.53981633974483
David Bailey@DAVIDBAILEY-PC /c/Go/src/golang-book/chapter9/chapterProblems
$
<pre><code>
再次,谢谢。