致命错误:所有goroutine都处于休眠状态-死锁

致命错误:所有goroutine都处于休眠状态-死锁,go,goroutine,Go,Goroutine,我有下面的Go代码 package main import ( "fmt" "math/rand" ) const ( ROCK int = iota PAPER SCISSORS ) type Choice struct { Who int //0 you 1 your opponent Guess int } //Win returns true if you win. func Win(you, he int) bool

我有下面的Go代码

package main

import (
    "fmt"
    "math/rand"
)

const (
    ROCK int = iota
    PAPER
    SCISSORS
)

type Choice struct {
    Who   int //0 you 1 your opponent
    Guess int
}

//Win returns true if you win.
func Win(you, he int) bool {
    ...
}

func Opponent(guess chan Choice, please chan struct{}) {
    for i := 0; i < 3; i++ {
        <-please
        choice := rand.Intn(3)
        who := 1
        guess <- Choice{who, choice}
        please <- struct{}{}
    }
}

func GetChoice(you, he int) int {
    ...
}

var Cheat func(guess chan Choice) chan Choice = func(guess chan Choice) chan Choice {
    new_guess := make(chan Choice)
    // go func() {
    for i := 0; i < 3; i++ {
        g1 := <-guess
        g2 := <-guess
        if g1.Who == 0 {
            choice := GetChoice(g1.Guess, g2.Guess)
            new_guess <- g2
            new_guess <- Choice{g1.Who, choice}
        } else {
            choice := GetChoice(g2.Guess, g1.Guess)
            new_guess <- g1
            new_guess <- Choice{g2.Who, choice}
        }
    }
    // }()
    fmt.Println("...")
    return new_guess
}

func Me(guess chan Choice, please chan struct{}) {

    for i := 0; i < 3; i++ {
        <-please
        choice := rand.Intn(3)
        who := 0
        guess <- Choice{who, choice}
        please <- struct{}{}
    }
}

func Game() []bool {
    guess := make(chan Choice)
    //please sync 2 goroutines.
    please := make(chan struct{})
    go func() { please <- struct{}{} }()
    go Opponent(guess, please)
    go Me(guess, please)
    guess = Cheat(guess)
    var wins []bool

    for i := 0; i < 3; i++ {
        g1 := <-guess
        g2 := <-guess
        win := false
        if g1.Who == 0 {
            win = Win(g1.Guess, g2.Guess)
        } else {
            win = Win(g2.Guess, g1.Guess)
        }
        wins = append(wins, win)
    }

    return wins
}

func main() {
    win := Game()
    fmt.Println(win)
}
主程序包
进口(
“fmt”
“数学/兰德”
)
常数(
岩石int=iota
纸张
剪刀
)
类型选择结构{
你是谁,你是你的对手
猜整数
}
//如果你赢了,Win返回true。
func Win(你,他)bool{
...
}
func对手(猜测chan选项,请chan结构{}){
对于i:=0;i<3;i++{

在此简化示例中:

func Cheat(guess chan Choice) chan Choice {
        new_guess := make(chan Choice)
        new_guess <- Choice{}
        <-guess
        return new_guess
}
func作弊(猜chan Choice)chan Choice{
新猜:=制造(成龙选择)

在这个简化的例子中,新的猜测:

func Cheat(guess chan Choice) chan Choice {
        new_guess := make(chan Choice)
        new_guess <- Choice{}
        <-guess
        return new_guess
}
func作弊(猜chan Choice)chan Choice{
新猜:=制造(成龙选择)
新猜想