I am trying to create multiple goroutines and keep them running concurrently. Then when a request comes in I want to identify one of them them and stop only that particular goroutine while the rest contiue
File 1
mm := remote_method.NewPlayerCreator()
mm.NewPlayer("Leo", "Messi")
// Lets just assume that the method call starts a bot which starts playing football
mm.NewPlayer("Cristiano", "Ronaldo")
mm.StopPlayer("Leo", "Messi")
File 2
package remote_method
type PlayerCreator struct {
playerNameChannelNumberMap (map[Player](chan int))
}
type Player struct {
firstName, lastName string
}
func NewPlayerCreator() DeclaredType {
var nameChannelMap (map[Player](chan int))
m := PlayerCreator{nameChannelMap}
return m
}
func (mm NewPlayerCreator) NewPlayer(firstName string, lastName string) {
// Update the playerNameChannelNumberMap to add this new Player in the map
// Since maps are basically called by reference, the map withe the file 1 should be updated or should it ?
// Call to a goroutine that would create a new player and the player keeps on running in an infinite loop
// I can add the goroutine code in this method and not create a new method for the goroutine, if it makes more sense like this
}
func (mm NewPlayerCreator) StopPlayer(firstName string, lastName string) {
// Find the player channel in the map and send a done signal on receiving which it exits
// Remove the player entry from the map
}
What are the best known practices for doing such a thing ? TIA