onetwothreeTicker ticked
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
)
func main() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("> ")
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
switch text {
case "one":
go timeTick(true, 1)
case "two":
go timeTick(true, 2)
case "three":
go timeTick(true, 3)
default:
go timeTick(false, 0)
}
}
}
func timeTick(flag bool, tick int) {
var tickChan *time.Ticker
if flag {
tickChan = time.NewTicker(time.Second * time.Duration(tick))
}
doneChan := make(chan bool)
if !flag {
doneChan <- true
}
for {
select {
case <-tickChan.C:
fmt.Println("Ticker ticked")
case <-doneChan:
fmt.Println("Done")
return
}
}
}
onetwothreetruedoneChan
oneTicker tickedtwothreedoneChan