从Go编译器中得到一个”undefined: distance”错误。

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

Getting an "undefined: distance" error from Go compiler

问题

我是一名经验丰富的“老派”程序员,但在Go语言方面是个初学者。我正在阅读《CreateSpace: An Introduction to Programing in Go》一书。在第9章的第3个问题中,任务是向用户定义的Shape接口添加一个新的方法。在本章中,接口已经逐步构建起来,以下是我目前的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type Shape interface {
  7. area() float64
  8. perimeter() float64
  9. }
  10. type Distance struct {
  11. x1, y1, x2, y2 float64
  12. }
  13. func (d *Distance) distance() float64 {
  14. a := d.x2 - d.x1
  15. b := d.y2 - d.y1
  16. return math.Sqrt(a*a + b*b)
  17. }
  18. type Rectangle struct {
  19. x1, y1, x2, y2 float64
  20. }
  21. func (r *Rectangle) area() float64 {
  22. l := distance(r.x1, r.y1, r.x2, r.y1)
  23. w := distance(r.x1, r.y1, r.x1, r.y2)
  24. return l * w
  25. }
  26. type Circle struct {
  27. x, y, r float64
  28. }
  29. func (c *Circle) area() float64 {
  30. return math.Pi * c.r * c.r
  31. }
  32. type Perimeter struct {
  33. x1, y1, x2, y2 float64
  34. }
  35. func (p *Perimeter) perimeter() float64 {
  36. s1 := distance(p.x1, p.y1, p.x1, p.y2)
  37. s2 := distance(p.x1, p.y2, p.x2, p.y2)
  38. s3 := distance(p.x2, p.y2, p.x2, p.y1)
  39. s4 := distance(p.x2, p.y1, p.x1, p.y1)
  40. return s1 + s2 + s3 + s4
  41. }
  42. func main() {
  43. d := new(Distance)
  44. d.x2, d.y2, d.x1, d.y1 = 0, 0, 10, 10
  45. p := new(Perimeter)
  46. p.x1, p.y1 = 0, 0
  47. p.x2, p.y2 = 10, 10
  48. fmt.Println(p.perimeter())
  49. r := new(Rectangle)
  50. r.x1, r.y1 = 0, 0
  51. r.x2, r.y2 = 10, 10
  52. fmt.Println(r.area())
  53. c := Circle{0, 0, 5}
  54. fmt.Println(c.area())
  55. }

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

  1. # command-line-arguments
  2. .\interface.go:25: undefined: distance
  3. .\interface.go:26: undefined: distance
  4. .\interface.go:42: undefined: distance
  5. .\interface.go:43: undefined: distance
  6. .\interface.go:44: undefined: distance
  7. .\interface.go:45: undefined: distance

我已经花了很多时间仔细阅读章节内容,并努力思考这个“undefined: distance”错误可能意味着什么,但迄今为止没有成功。

正如你所看到的,我定义了一个“Distance结构体”,创建了一个名为d的新实例,并使用.运算符初始化了它的字段,并创建了一个distance()函数,但显然,我没有理解一些相关的信息。

英文:

I'm a seasoned "old school" programmer, but a rank beginner with Go. I'm making my way through the "CreateSpace: An Introduction to
Programing in Go" book. On p. 111, the third of Chapter 9's Chapter Problems, the task is to add a new method to the user-defined Shape interface. The interface has been built up over the course of the chapter and here's what I have so far:

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. type Shape interface {
  7. area() float64
  8. perimeter() float64
  9. }
  10. type Distance struct {
  11. x1, y1, x2, y2 float64
  12. }
  13. func (d *Distance) distance() float64 {
  14. a := d.x2 - d.x1
  15. b := d.y2 - d.y1
  16. return math.Sqrt(a*a + b*b)
  17. }
  18. type Rectangle struct {
  19. x1, y1, x2, y2 float64
  20. }
  21. func (r *Rectangle) area() float64 {
  22. l := distance(r.x1, r.y1, r.x2, r.y1)
  23. w := distance(r.x1, r.y1, r.x1, r.y2)
  24. return l * w
  25. }
  26. type Circle struct {
  27. x, y, r float64
  28. }
  29. func (c *Circle) area() float64 {
  30. return math.Pi * c.r * c.r
  31. }
  32. type Perimeter struct {
  33. x1, y1, x2, y2 float64
  34. }
  35. func (p *Perimeter) perimeter() float64 {
  36. s1 := distance(p.x1, p.y1, p.x1, p.y2)
  37. s2 := distance(p.x1, p.y2, p.x2, p.y2)
  38. s3 := distance(p.x2, p.y2, p.x2, p.y1)
  39. s4 := distance(p.x2, p.y1, p.x1, p.y1)
  40. return s1 + s2 + s3 + s4
  41. }
  42. func main() {
  43. d := new(Distance)
  44. d.x2, d.y2, d.x1, d.y1 = 0, 0, 10, 10
  45. p := new(Perimeter)
  46. p.x1, p.y1 = 0, 0
  47. p.x2, p.y2 = 10, 10
  48. fmt.Println(p.perimeter())
  49. r := new(Rectangle)
  50. r.x1, r.y1 = 0, 0
  51. r.x2, r.y2 = 10, 10
  52. fmt.Println(r.area())
  53. c := Circle{0, 0, 5}
  54. fmt.Println(c.area())
  55. }

The problem is that I am getting the following error (from the compiler?):

  1. user@pc /c/Go/src/golang-book/chapter9/chapterProblems
  2. $ go run interface.go
  3. # command-line-arguments
  4. .\interface.go:25: undefined: distance
  5. .\interface.go:26: undefined: distance
  6. .\interface.go:42: undefined: distance
  7. .\interface.go:43: undefined: distance
  8. .\interface.go:44: undefined: distance
  9. .\interface.go:45: undefined: distance

I've spent a good amount of time doing my "due diligence" hereto pertaining by rereading the chapter text and thinking hard about what this
"undefined: distance" error might mean, but so far to no avail.
As you can see, I have defined a "Distance struct", created a new() instance of
it called d, initialized its fields with the . operator, and created a distance() func, but, clearly, I'm not grokking some relevant piece(s) of information.

答案1

得分: 2

你没有一个名为distance的函数。它是*Distance类型的方法。你需要先创建一个*Distance,然后调用该方法。

  1. d := &Distance{r.x1, r.y1, r.x2, r.y1}
  2. l := d.distance()

我建议你从Effective Go开始。这是一个非常好的介绍,适合“经验丰富的程序员”。

英文:

You don't have a function called distance. It's a method of the type *Distance. You need to create a *Distance first, and then call the method.

  1. d := &Distance{r.x1, r.y1, r.x2, r.y1}
  2. l := d.distance()

I'd suggest starting with Effective Go. It's a very good introduction into the language for "a seasoned programmer".

答案2

得分: 1

你在这里定义的函数:

  1. func (d *Distance) distance() float64 {
  2. a := d.x2 - d.x1
  3. b := d.y2 - d.y1
  4. return math.Sqrt(a*a + b*b)
  5. }

是 Distance 对象的一个方法。看起来你想在这里创建一个新的 Distance 实例:

  1. func (r *Rectangle) area() float64 {
  2. l := distance(r.x1, r.y1, r.x2, r.y1)
  3. w := distance(r.x1, r.y1, r.x1, r.y2)
  4. return l.distance() * w.distance()
  5. }

但实际上你正在尝试调用一个名为 distance 的函数。

你想要的是:

  1. func (r *Rectangle) area() float64 {
  2. l := &Distance{r.x1, r.y1, r.x2, r.y1}
  3. w := &Distance{r.x1, r.y1, r.x1, r.y2}
  4. return l.distance() * w.distance()
  5. }
英文:

Your function defined here:

  1. func (d *Distance) distance() float64 {
  2. a := d.x2 - d.x1
  3. b := d.y2 - d.y1
  4. return math.Sqrt(a*a + b*b)
  5. }

is a method on the Distance object. It looks like you're trying to create a new Distance instance here:

  1. func (r *Rectangle) area() float64 {
  2. l := distance(r.x1, r.y1, r.x2, r.y1)
  3. w := distance(r.x1, r.y1, r.x1, r.y2)
  4. return l.distance() * w.distance()
  5. }

but what you're actually doing is trying to call a function called distance.

You want

  1. func (r *Rectangle) area() float64 {
  2. l := &Distance{r.x1, r.y1, r.x2, r.y1}
  3. w := &Distance{r.x1, r.y1, r.x1, r.y2}
  4. return l.distance() * w.distance()
  5. }

答案3

得分: 1

感谢 @Ainar-G 和 @Momer!在我调整好了思路之后(还得修复了几个自己造成的语法错误),以下代码可以正常运行:

  1. package main
  2. import ("fmt"; "math")
  3. type Shape interface {
  4. area() float64
  5. perimeter() float64
  6. }
  7. type Distance struct {
  8. x1, y1, x2, y2 float64
  9. }
  10. func distance(x1, y1, x2, y2 float64) float64 {
  11. a := x2 - x1
  12. b := y2 - y1
  13. return math.Sqrt(a*a + b*b)
  14. }
  15. type Rectangle struct {
  16. x1, y1, x2, y2 float64
  17. }
  18. func (r *Rectangle) area() float64 {
  19. l := distance(r.x1, r.y1, r.x2, r.y1)
  20. w := distance(r.x1, r.y1, r.x1, r.y2)
  21. return l * w
  22. }
  23. type Circle struct {
  24. x, y, r float64
  25. }
  26. func (c *Circle) area() float64 {
  27. return math.Pi * c.r*c.r
  28. }
  29. type Perimeter struct {
  30. x1, y1, x2, y2 float64
  31. }
  32. func (p *Perimeter) perimeter() float64 {
  33. s1 := distance(p.x1, p.y1, p.x1, p.y2)
  34. s2 := distance(p.x1, p.y2, p.x2, p.y2)
  35. s3 := distance(p.x2, p.y2, p.x2, p.y1)
  36. s4 := distance(p.x2, p.y1, p.x1, p.y1)
  37. return s1 + s2 + s3 + s4
  38. }
  39. func main() {
  40. d := new(Distance)
  41. d.x1, d.y1, d.x2, d.y2 = 0, 0, 10, 10
  42. p := new(Perimeter)
  43. p.x1, p.y1, p.x2, p.y2 = 0, 0, 10, 10
  44. fmt.Println(p.perimeter())
  45. r := new(Rectangle)
  46. r.x1, r.y1 = 0, 0
  47. r.x2, r.y2 = 10, 10
  48. fmt.Println(r.area())
  49. c := new(Circle)
  50. c.x, c.y, c.r = 0, 0, 5
  51. fmt.Println(c.area())
  52. }

以下是运行结果:

  1. David Bailey@DAVIDBAILEY-PC /c/Go/src/golang-book/chapter9/chapterProblems
  2. $ go run interface.go
  3. 40
  4. 100
  5. 78.53981633974483
  6. David Bailey@DAVIDBAILEY-PC /c/Go/src/golang-book/chapter9/chapterProblems
  7. $

再次感谢你。

英文:

thanks @Ainar-G and @Momer! After I got my "mind right" (had to fix a couple more self-inflicted syntax errors), the following worked:

  1. <pre><code>
  2. package main
  3. import ("fmt"; "math")
  4. type Shape interface {
  5. area() float64
  6. perimeter() float64
  7. }
  8. type Distance struct {
  9. x1, y1, x2, y2 float64
  10. }
  11. func distance(x1, y1, x2, y2 float64) float64 {
  12. a := x2 - x1
  13. b := y2 - y1
  14. return math.Sqrt(a*a + b*b)
  15. }
  16. type Rectangle struct {
  17. x1, y1, x2, y2 float64
  18. }
  19. func (r *Rectangle) area() float64 {
  20. l := distance(r.x1, r.y1, r.x2, r.y1)
  21. w := distance(r.x1, r.y1, r.x1, r.y2)
  22. return l * w
  23. }
  24. type Circle struct {
  25. x, y, r float64
  26. }
  27. func (c *Circle) area() float64 {
  28. return math.Pi * c.r*c.r
  29. }
  30. type Perimeter struct {
  31. x1, y1, x2, y2 float64
  32. }
  33. func (p *Perimeter) perimeter() float64 {
  34. s1 := distance(p.x1, p.y1, p.x1, p.y2)
  35. s2 := distance(p.x1, p.y2, p.x2, p.y2)
  36. s3 := distance(p.x2, p.y2, p.x2, p.y1)
  37. s4 := distance(p.x2, p.y1, p.x1, p.y1)
  38. return s1 + s2 + s3 + s4
  39. }
  40. func main() {
  41. d := new(Distance)
  42. d.x1, d.y1, d.x2, d.y2 = 0, 0, 10, 10
  43. p := new(Perimeter)
  44. p.x1, p.y1, p.x2, p.y2 = 0, 0, 10, 10
  45. fmt.Println(p.perimeter())
  46. r := new(Rectangle)
  47. r.x1, r.y1 = 0, 0
  48. r.x2, r.y2 = 10, 10
  49. fmt.Println(r.area())
  50. c := new(Circle)
  51. c.x, c.y, c.r = 0, 0, 5
  52. fmt.Println(c.area())
  53. }
  54. <pre><code>

Here's the resulting output:

  1. <pre><code>
  2. David Bailey@DAVIDBAILEY-PC /c/Go/src/golang-book/chapter9/chapterProblems
  3. $ go run interface.go
  4. 40
  5. 100
  6. 78.53981633974483
  7. David Bailey@DAVIDBAILEY-PC /c/Go/src/golang-book/chapter9/chapterProblems
  8. $
  9. <pre><code>

Again, thank you.

huangapple
  • 本文由 发表于 2015年7月1日 21:54:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/31163376.html
匿名

发表评论

匿名网友

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

确定