周末剛開始使用 Go,我不確定我是否正確使用了 Go 的特性,或者我是否根本沒有做過這種“類似 Go”的工作。該代碼應在稱為的地圖元素上進行迭代non_placed_alleles,并將每個元素與中的所有元素進行比較,這些元素placed_alleles也存儲在地圖中。我正在嘗試對每個元素使用一個go-routine,non_placed_alleles因為比較成本很高,而且要花很長時間。這是主函數的一些內容:runtime.GOMAXPROCS(8) // For 8 consecutive routines at once? got 10 CPUsc := make(chan string)for name, alleles := range non_placed_alleles { go get_best_places(name, alleles, &placed_alleles, c) // pointer to placed_alleles as we only read, not write - should be safe?}for channel_item := range c { fmt.Println("This came back ", channel_item)} // This also crashes with "all goroutines are sleeping", // but all results are printed這是被調用的函數:func get_best_places(name string, alleles []string, placed_alleles *map[string] []string, c chan string) { var best_partner string // Iterate over all elements of placed_alleles, find best "partner" for other_key, other_value := range *placed_alleles { best_partner := compare_magic() // omitted because boring } c <- best_partner}有什么辦法可以使這個“更好”?快點?我是否正確使用了指針魔術和 goroutines?
- 1 回答
- 0 關注
- 186 瀏覽
添加回答
舉報
0/150
提交
取消