1
서로 정수를 보내는 세 개의 동시 루틴을 작성하려고합니다. 이제 정수를 서로 전송하는 두 개의 동시 루틴을 구현했습니다. 나는 위의 루틴으로/정수를 송수신하고자하는 다른 루틴을 추가 할 때Go의 동시 루틴
package main
import "rand"
func Routine1(commands chan int, responses chan int) {
for i := 0; i < 10; i++ {
i := rand.Intn(100)
commands <- i
print(<-responses, " 1st\n");
}
close(commands)
}
func Routine2(commands chan int, responses chan int) {
for i := 0; i < 1000; i++ {
x, open := <-commands
if !open {
return;
}
print(x , " 2nd\n");
y := rand.Intn(100)
responses <- y
}
}
func main()
{
commands := make(chan int)
responses := make(chan int)
go Routine1(commands, responses)
Routine2(commands, responses)
}
그러나, 그것은! "- 교착 모든 goroutines이 잠 던져"와 같은 오류를 제공합니다. 내 실수
package main
import "rand"
func Routine1(commands chan int, responses chan int, command chan int, response chan int) {
for i := 0; i < 10; i++ {
i := rand.Intn(100)
commands <- i
command <- i
print(<-responses, " 12st\n");
print(<-response, " 13st\n");
}
close(commands)
}
func Routine2(commands chan int, responses chan int) {
for i := 0; i < 1000; i++ {
x, open := <-commands
if !open {
return;
}
print(x , " 2nd\n");
y := rand.Intn(100)
responses <- y
}
}
func Routine3(command chan int, response chan int) {
for i := 0; i < 1000; i++ {
x, open := <-command
if !open {
return;
}
print(x , " 3nd\n");
y := rand.Intn(100)
response <- y
}
}
func main() {
commands := make(chan int)
responses := make(chan int)
command := make(chan int)
response := make(chan int)
go Routine1(commands, responses,command, response)
Routine2(commands, responses)
Routine3(command, response)
}
아무도 도와 줄 수
입니다 : 다음은 내 코드는? 그리고 아무도 나를 도울 수있다, 양방향 채널을 만들 수 있습니까 아니면 int, string 등의 공통 채널을 만들 수 있습니까?
수정. Go는'command'와'commands'를 다른 변수로 취급하고,'command'를 선언하지 않았습니다. 비슷한 변수 이름을 감지하고 연결하는 기능은 Go 언어에 없습니다. –
죄송합니다. 실수로. 그러나, 내 질문을 변경했습니다. 그리고 추가 질문은 양방향 채널을 만들 수 있습니까? int, string 등의 공통 채널을 생성 할 수 있습니까? – Arpssss