【发布时间】:2022-01-21 11:21:08
【问题描述】:
我在函数 FindPathConcurently 中收到此错误。由于我是 Go 新手,我正在尝试理解它,但无法提供任何帮助,我将不胜感激。我正在尝试解决我正在为迷宫求解器编写代码的问题。 Node 只是一个带有 next 和 value 的结构体。
func FindExitConcurrently(root []*Node, paths chan []string, path []string, index int) {
for _, node := range root {
if node.Value == consts.SUCCESS {
paths <- path
return
}
if node.Next == nil {
path = nil
return
}
path = append(path, node.Value)
index++
FindExitConcurrently(node.Next, paths, path, index)
path = nil
index--
}
if index == 0 {
close(paths)
}
}
func FindPathConcurrently(parentNode *Node) {
var pathChannels []chan []string
for key, value := range parentNode.Next {
pathChannels = append(pathChannels, make(chan []string))
go FindExitConcurrently(value.Next, pathChannels[key], nil, 0)
}
cases := make([]reflect.SelectCase, len(pathChannels))
for i, ch := range pathChannels {
cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ch)}
}
remaining := len(cases)
for remaining > 0 {
chosen, value, ok := reflect.Select(cases)
if !ok {
// The chosen channel has been closed, so zero out the channel to disable the case
cases[chosen].Chan = reflect.ValueOf(nil)
remaining -= 1
continue
}
//fmt.Printf("Read from channel %#v and received %s\n", pathChannels[chosen], value.String())
fmt.Println(value)
}
}
【问题讨论】: