For loop 在第一次间隔前优雅地运行股票代码器的主体?

For loop 在第一次间隔前优雅地运行股票代码器的主体?,for-loop,go,timer,For Loop,Go,Timer,新功能,我实现了一个以给定间隔轮询API的小程序: func Poll() <-chan map[uint8]Data { json := make(chan map[uint8]Data) user, pass, endpoint := credentials.Get() ticker := time.NewTicker(90 * time.Second) client := &http.Client{} req, _ := http

新功能,我实现了一个以给定间隔轮询API的小程序:

func Poll() <-chan map[uint8]Data {
    json := make(chan map[uint8]Data)

    user, pass, endpoint := credentials.Get()

    ticker := time.NewTicker(90 * time.Second)

    client := &http.Client{}
    req, _ := http.NewRequest("GET", endpoint, nil)
    req.SetBasicAuth(user, pass)

    go func() {
        for range ticker.C {
            resp, _ := client.Do(req)
            bodyText, _ := ioutil.ReadAll(resp.Body)
            json <- extract(string(bodyText))
        }
    }()

    return json
}

func Poll()

for ; true; <-ticker.C {
    resp, _ := client.Do(req)
    bodyText, _ := ioutil.ReadAll(resp.Body)
    json <- extract(string(bodyText))
}

for;是的;这是一件美丽的事情,正是我所寻找的;谢谢您可以进一步简化为
for;;
for ; true; <-ticker.C {
    resp, _ := client.Do(req)
    bodyText, _ := ioutil.ReadAll(resp.Body)
    json <- extract(string(bodyText))
}
t := time.NewTicker(2 * time.Second)
now := time.Now()
for ; true; <-t.C {
    fmt.Println(time.Since(now))
}