【问题标题】:GoLang printf shows nothing, even after using time.Sleep [duplicate]即使使用 time.Sleep [重复],GoLang printf 也没有显示任何内容
【发布时间】:2016-05-14 13:02:18
【问题描述】:

我一直想知道为什么retiredOpCode 函数中的fmt.Print 不起作用。虽然,dispatchOpCode 函数中的 fmt.Print 工作正常。 下面的代码是完整的代码(您可以尝试从 go playground 运行)并且 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)
}

说实话,我真的不知道为什么 Print 不起作用。例如 dispatchOpCode 中的打印,如果我将 Main Go Routine 更改为以下内容,它将不起作用:

for j:=0;j<totalNumber;j++{
        go generateOpCode(opCode)
        go dispatchOpCode(opCode, toPipeline, nextPipeline)
        go fmt.Println("=========================================")
    }

有谁知道为什么它不起作用?或者用更正的代码解释这个?谢谢!

【问题讨论】:

  • 您以前用 Java 编写过代码吗?出于某种原因,您的代码使 Go 看起来像 Java。请尝试分隔行,即使您可以使用;
  • 我建议阅读一下Go pipeline pattern 以在阶段和通道之间编写更清晰的同步。
  • @PieOhPah,谢谢你的建议,我看看!

标签: go


【解决方案1】:

你从来没有在fromPipeline里放任何东西。

如果我在任何地方添加fromPipeline[1] &lt;- 2 行,我会看到Complete : 2 By Pipeline : 1 打印出来,因为retiredOpCode 正常工作。

【讨论】:

  • 你把 fromPipeline[1]
  • 我只是把它放在 main() 中 for 循环末尾的 fmt.Println() 之后。
猜你喜欢
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
  • 2015-02-02
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
相关资源
最近更新 更多