我是一名经验丰富的“守旧派”程序员,但是是 Go 的新手。我正在学习“CreateSpace:介绍 Programming in Go”一书。在第 111 页,第 9 章问题的第三部分,任务是向用户定义的 Shape 接口(interface)添加一个新方法。该接口(interface)是在本章的过程中构建的,下面是我到目前为止:
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”错误可能意味着,但到目前为止无济于事。
如您所见,我定义了一个“距离结构”,创建了一个 new() 实例
它调用了 d,用 . 运算符初始化了它的字段,并创建了一个 distance() 函数,但是,很明显,我不是在摸索一些相关的信息。
最佳答案
您没有名为distance 的函数。它是 *Distance 类型的方法。您需要先创建一个*Distance,然后调用该方法。
d := &Distance{r.x1, r.y1, r.x2, r.y1}
l := d.distance()
我建议从 Effective Go 开始.对于“经验丰富的程序员”来说,这是对这门语言的非常好的介绍。
关于go - 从 Go 编译器获取 "undefined: distance"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31163376/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
似乎无法为此找到有效的答案。我正在阅读Rails教程的第10章第10.1.2节,但似乎无法使邮件程序预览正常工作。我发现处理错误的所有答案都与教程的不同部分相关,我假设我犯的错误正盯着我的脸。我已经完成并将教程中的代码复制/粘贴到相关文件中,但到目前为止,我还看不出我输入的内容与教程中的内容有什么区别。到目前为止,建议是在函数定义中添加或删除参数user,但这并没有解决问题。触发错误的url是http://localhost:3000/rails/mailers/user_mailer/account_activation.http://localhost:3000/rails/mai
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url