just started working with Go over the weekend and I'm unsure whether I used Go's peculiarities right, or whether I haven't done this "Go-like" at all.
The code is supposed to iterate over elements of a map called non_placed_alleles
and compare each of these with all elements in placed_alleles
, which are stored in a map as well. I'm trying to use one go-routine for each of the elements in non_placed_alleles
as the comparison is quite costly and takes forever.
Here's a bit from the main-function:
runtime.GOMAXPROCS(8) // For 8 consecutive routines at once? got 10 CPUs
c := 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
And here's the called function:
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
}
Is there any way to make this "better"? Faster? Have I used the pointer-magic and goroutines correctly?