for select 與 for range

可以使用下面兩方法持續監聽 channel

for select

package main
import (
    "fmt"
    "time"
)

func main() {
    ticker := time.NewTicker(1 * time.Second)
	for {
	  select {
	    case <-ticker.C:
		  fmt.Println("test")  
	  }
	}
}

for range

package main
import (
    "fmt"
    "time"
)

func main() {
    ticker := time.NewTicker(1 * time.Second)
    for range ticker.C {
      fmt.Println("test")  
    }
}

Last updated