怎么才能让一个go程序一直运行呢

cups_book · · 3815 次点击
你需要了解下select 分别case两个chan 一个是timer的chan 一个是ticker的chan 大概是这样 go func(){ select{ case <-ticker.c: dosomething() case <-timer.c: break; } }()
#10
更多评论
把业务加到For 循环中
#1
xmge
欲戴王冠 必承其重
```go package main import ( "log" "net/http" "os" "time" ) // 检测百度接口 var myLog = log.New(os.Stdout,"[api 检测] ",log.Lmicroseconds) func main() { for { time.Sleep(5 * time.Second) resp,err := http.Get("http://www.baidu.com") if err != nil { myLog.Println("请求百度错误:",err) continue } if resp.StatusCode != 200 { myLog.Println("百度出现故障,code:",resp.StatusCode) continue } myLog.Println("百度运行正常") } } ```
#2