Skip to content

Instantly share code, notes, and snippets.

@yaegaki
Created February 18, 2016 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaegaki/da4544c51dedef5574be to your computer and use it in GitHub Desktop.
Save yaegaki/da4544c51dedef5574be to your computer and use it in GitHub Desktop.
forをchanを使って途中で止める(スレッドセーフ)
package main
import (
"log"
"time"
)
func temp(quitChan chan bool) {
for i := 0; i <= 1000000; i++ {
select {
case <-quitChan:
return
default:
log.Println(i)
}
time.Sleep(10 * time.Millisecond)
}
}
func main() {
c := make(chan bool)
go temp(c)
time.Sleep(2 * time.Second)
close(c)
time.Sleep(2 * time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment