第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在函數(shù)中初始化通道導(dǎo)致 go routine 死鎖

在函數(shù)中初始化通道導(dǎo)致 go routine 死鎖

Go
慕妹3146593 2023-05-04 17:29:24
我是 Go 的新手,所以我確定這是我所缺少的簡(jiǎn)單東西。我正在嘗試初始化一個(gè)通道以從另一個(gè)函數(shù)捕獲用戶輸入。我嘗試了以下內(nèi)容:package inputconst UP = 1const RIGHT = 2const DOWN =3const LEFT = 4var inputChannel chan inttype InputReader interface {  ReadNextInt() int}func InitInputChannel() chan int {  inputChannel := make(chan int, 1)  return inputChannel}func SendInput(inputReader InputReader) {    inputChannel <- inputReader.ReadNextInt()}然后我調(diào)用了以下代碼:package inputimport (  "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)}查看代碼我無(wú)法找出問題所在,所以我決定重組一些東西,因?yàn)槲乙呀?jīng)絕望了。我最終得到了以下有效的方法:package inputconst UP = 1const RIGHT = 2const DOWN =3const LEFT = 4var inputChannel chan int = make(chan int, 1)type InputReader interface {  ReadNextInt() int}func SendInput(inputReader InputReader) chan int {    inputChannel <- inputReader.ReadNextInt()    return inputChannel}雖然我很高興它能正常工作,但我很困惑為什么我的第一個(gè)解決方案不起作用。當(dāng)只需要抓取一次時(shí),我也不太愿意為每次 SendInput 調(diào)用返回我的頻道。也許 'InputChannel() chan int' getter 會(huì)更好?有什么見解嗎?謝謝
查看完整描述

1 回答

?
暮色呼如

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊

使用了不正確的變量聲明形式。所以應(yīng)該做這樣的事情:


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()

}

要注意的關(guān)鍵是“ inputChannel = make(.....) ”,而不是像我之前嘗試的那樣“ inputChannel := make(....) ”。


查看完整回答
反對(duì) 回復(fù) 2023-05-04
  • 1 回答
  • 0 關(guān)注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)