我是 Go 的新手,所以我确定这是我所缺少的简单内容。我正在尝试初始化一个 channel 以从另一个函数捕获用户输入。我尝试了以下方法:
package input
const UP = 1
const RIGHT = 2
const DOWN =3
const LEFT = 4
var inputChannel chan int
type InputReader interface {
ReadNextInt() int
}
func InitInputChannel() chan int {
inputChannel := make(chan int, 1)
return inputChannel
}
func SendInput(inputReader InputReader) {
inputChannel <- inputReader.ReadNextInt()
}
然后我调用了以下代码:
package input
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type MockedInputReader struct {
mock.Mock
}
func (reader MockedInputReader) ReadNextInt() int {
return 1
}
func TestShouldSendUpValueToChannelWhenUpKeyPressed(t *testing.T) {
inputReader := new(MockedInputReader)
inputReader.On("ReadNextInt").Return(UP)
receiverChannel := SendInput(inputReader)
actualInput := <- receiverChannel
assert.Equal(t, UP, actualInput)
}
查看代码我无法找出问题所在,所以我决定重组一些东西,因为我已经绝望了。我最终得到了以下有效的方法:
package input
const UP = 1
const RIGHT = 2
const DOWN =3
const LEFT = 4
var inputChannel chan int = make(chan int, 1)
type InputReader interface {
ReadNextInt() int
}
func SendInput(inputReader InputReader) chan int {
inputChannel <- inputReader.ReadNextInt()
return inputChannel
}
虽然我很高兴它能正常工作,但我很困惑为什么我的第一个解决方案不起作用。当只需要抓取一次时,我也不太愿意为每次 SendInput 调用返回我的 channel 。也许 'InputChannel() chan int' getter 会更好?有什么见解吗?谢谢
最佳答案
正如 ThunderCat 在对我的问题的评论中提到的,我使用了不正确的变量声明形式。所以我应该做这样的事情:
package input
const UP = 1
const RIGHT = 2
const DOWN = 3
const LEFT = 4
var inputChannel chan int
type InputReader interface {
ReadNextInt() int
}
func InitChan() chan int {
inputChannel = make(chan int, 1)
return inputChannel
}
func SendInput(inputReader InputReader) {
inputChannel <- inputReader.ReadNextInt()
}
要注意的关键是“inputChannel = make(.....)”而不是“inputChannel := make(。 ...)' 就像我以前尝试过的那样。
关于go - 在函数中初始化 channel 导致 go routine 死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53093089/
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc
我正在阅读一本关于Ruby的书,作者在编写类初始化定义时使用的形式与他在本书前几节中使用的形式略有不同。它看起来像这样:classTicketattr_accessor:venue,:datedefinitialize(venue,date)self.venue=venueself.date=dateendend在本书的前几节中,它的定义如下:classTicketattr_accessor:venue,:datedefinitialize(venue,date)@venue=venue@date=dateendend在第一个示例中使用setter方法与在第二个示例中使用实例变量之间是
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
说在前面这部分我本来是合为一篇来写的,因为目的是一样的,都是通过独立按键来控制LED闪灭本质上是起到开关的作用,即调用函数和中断函数。但是写一篇太累了,我还是决定分为两篇写,这篇是调用函数篇。在本篇中你主要看到这些东西!!!1.调用函数的方法(主要讲语法和格式)2.独立按键如何控制LED亮灭3.程序中的一些细节(软件消抖等)1.调用函数的方法思路还是比较清晰地,就是通过按下按键来控制LED闪灭,即每按下一次,LED取反一次。重要的是,把按键与LED联系在一起。我打算用K1来作为开关,看了一下开发板原理图,K1连接的是单片机的P31口,当按下K1时,P31是与GND相连的,也就是说,当我按下去时
我需要一个通过输入字符串进行计算的方法,像这样function="(a/b)*100"a=25b=50function.something>>50有什么方法吗? 最佳答案 您可以使用instance_eval:function="(a/b)*100"a=25.0b=50instance_evalfunction#=>50.0请注意,使用eval本质上是不安全的,尤其是当您使用外部输入时,因为它可能包含注入(inject)的恶意代码。另请注意,a设置为25.0而不是25,因为如果它是整数a/b将导致0(整数)。