2012-10-22 3 views
1

나는이 때문에 교착 상태에 첫 번째 명령 행 입력 후에 보석금 다음과 같은 최소한의 예 :단일 채널 선택 문 교착 상태

난 그냥 사용자 입력을하고이를 통해 에코 단순히이 예상 한 것
package main  

import "fmt"  
import "os"  

func main() {  
    channel1 := make(chan string)  

    go func() {  
     var str string  
     for {    
      fmt.Fscanln(os.Stdin, &str)  
      channel1 <- str     
     }      
    }()  

    for {  
     select {  
     case str := <-channel1:  
      fmt.Printf("Channel1 said: %v\n", str)  
     }            
    }    
} 

그리고 다시. 또한 두 번째 채널을 추가하고 두 번째 이동 루틴에 데드락 문제가 없다는 것을 알았습니다. 그럼 왜이 교착 상태에 빠졌습니까?

답변

1

문제를 재현 할 수 없습니다.

[email protected]:~/src/tmp/SO/13015469$ cat main.go 
package main 

import (
    "fmt" 
    "os" 
) 

func main() { 
    channel1 := make(chan string) 

    go func() { 
     var str string 
     for { 
      fmt.Fscanln(os.Stdin, &str) 
      channel1 <- str 
     } 
    }() 

    for { 
     select { 
     case str := <-channel1: 
      fmt.Printf("Channel1 said: %v\n", str) 
     } 
    } 
} 
[email protected]:~/src/tmp/SO/13015469$ go run main.go 
foo 
Channel1 said: foo 
bar 
Channel1 said: bar 
baz 
Channel1 said: baz 
^[email protected]:~/src/tmp/SO/13015469$ go build main.go && ./main 
foo 
Channel1 said: foo 
bar 
Channel1 said: bar 
baz 
Channel1 said: baz 
^[email protected]:~/src/tmp/SO/13015469$ go version 
go version go1.0.3 
[email protected]:~/src/tmp/SO/13015469$ uname -a 
Linux fsc-r630 3.2.0-32-generiC#51-Ubuntu SMP Wed Sep 26 21:33:09 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux 
[email protected]:~/src/tmp/SO/13015469$ 

Go 버전 OS은 무엇입니까?

+0

1.0.2를 실행 중이므로 1.0.3으로 업그레이드하면 문제가 해결 된 것으로 보입니다. 저를 올바른 방향으로 가리켜 주셔서 감사합니다! – Chris