我一直想知道为什么retiredOpCode函数中的fmt.Print无效。虽然,dispatchOpCode函数中的fmt.Print工作正常。 下面的代码是完整的代码(您可以尝试从go操场上运行),dispatchOpCode函数中的fmt.Print正在运行。
package main
import ("fmt"
"time"
"math/rand")
var maxPipeline = 3 //Max Pipeline
var maxNumber = 5 //Max Number which will be randomed
const totalNumber = 10 //Total number will be randomed
//========================================================================
// FUNCTION TO DELAY THE TIME (TO PREVENT GO FROM EXIST AFTER GO ROUTINE)
//========================================================================
func delayTime(multiplier time.Duration){
time.Sleep(time.Millisecond * multiplier)
}
//========================================================================
// GENERATE THE OPCODES
//========================================================================
func generateOpCode(opCode chan int){
opCode <- rand.Intn(maxNumber) + 1 //Generate the random number with consistent sequence
}
//========================================================================
// DISPATCH THE OPCODE
//========================================================================
func dispatchOpCode(opCode chan int, toPipeline []chan int, nextPipeline []chan bool){
tempPipeline := 0 //Temporary for choosing which pipeline
select{ //Assign the OP Code to the ready pipeline, and pass the OP Code to the selected pipeline
case <- nextPipeline[0] : toPipeline[0] = opCode ; tempPipeline = 0
case <- nextPipeline[1] : toPipeline[1] = opCode ; tempPipeline = 1
case <- nextPipeline[2] : toPipeline[2] = opCode ; tempPipeline = 2
}
fmt.Println("Op Code :",<-opCode," To Pipeline :",tempPipeline)
}
//========================================================================
// PROCESS THE PIPELINE TO RETIRE FUNCTION
//========================================================================
func pipelineProcess(fromDispatcher chan int, retireOpCode chan int,nextPipeline chan bool){
retireOpCode = fromDispatcher //Pass the opCode to the retire Op Code
nextPipeline <- true //Tell the pipeline used it's finished
}
//========================================================================
// RETIRE THE OP CODE
//========================================================================
func retiredOpCode(fromPipeline []chan int){
tempPipeline := 0 //Temporary for choosing which pipeline used
select{ //Read the retired code from selected pipeline and assign the used pipeline
case opCodes :=<- fromPipeline[0] : tempPipeline = 0 ; fmt.Println("Complete :",opCodes," By Pipeline :",tempPipeline)
case opCodes :=<- fromPipeline[1] : tempPipeline = 1 ; fmt.Println("Complete :",opCodes," By Pipeline :",tempPipeline)
case opCodes :=<- fromPipeline[2] : tempPipeline = 2 ; fmt.Println("Complete :",opCodes," By Pipeline :",tempPipeline)
}
}
//========================================================================
// MAIN FUNCTION
//========================================================================
func main(){
//=============== Create Channels ===============
opCode := make(chan int,1)
toPipeline := make([]chan int, maxPipeline)
for i:= range toPipeline{
toPipeline[i] = make(chan int)
}
fromPipeline := make([]chan int, maxPipeline)
for i:= range fromPipeline{
fromPipeline[i] = make(chan int)
}
nextPipeline := make([]chan bool, maxPipeline)
for i:= range nextPipeline{
nextPipeline[i] = make(chan bool)
}
//=============== Main Go Routine ===============
for j:=0;j<totalNumber;j++{
go generateOpCode(opCode)
go dispatchOpCode(opCode, toPipeline, nextPipeline)
for i:=0;i<maxPipeline;i++{
go pipelineProcess(toPipeline[i],fromPipeline[i],nextPipeline[i])
}
go retiredOpCode(fromPipeline)
go fmt.Println("=========================================")
}
delayTime(1000)
}
说实话,我真的不知道为什么打印不起作用。例如,dispatchOpCode中的print,如果我将Main Go Routine改为:
,它将不起作用:for j:=0;j<totalNumber;j++{
go generateOpCode(opCode)
go dispatchOpCode(opCode, toPipeline, nextPipeline)
go fmt.Println("=========================================")
}
有谁知道为什么它不起作用?或者用纠正后的代码解释一下这个问题?谢谢!
答案 0 :(得分:1)
你从未在fromPipeline
中添加任何内容。
如果我在任何地方添加了fromPipeline[1] <- 2
行,我会看到Complete : 2 By Pipeline : 1
打印出retiredOpCode
来完成其工作。