I need to create a html page which has two buttons, one will start a goroutine, which has an infinite loop, and other button need to break the infinite loop. As from my readings I understood that a goroutine can't be killed from outside it. Is there anyway to implement this? My code is as follows:
command := c.GetString("command") // from client to determine which button is clicked
quit := make(chan bool)
switch command {
case "start": // Button to start goroutine
go func() {
i := 0
for {
select {
case <- quit:
return
default:
fmt.Println("i: ", i)
i++
time.Sleep(3000 * time.Millisecond)
}
}
}()
case "stop": // Button to stop goroutine
quit <- true
}