定期轮询Go中的REST端点 [英] Periodically polling a REST endpoint in Go
本文介绍了定期轮询Go中的REST端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个Go应用程序,该应用程序定期轮询PHP应用程序公开的REST端点.Go轮询应用程序将有效负载读取到结构中并进行进一步处理.我正在寻找一些开始实施的建议.
I am trying to write a Go application that periodically polls a REST endpoint exposed by a PHP application. The Go polling application reads the payload into a struct and does further processing. I am looking for some recommendations for starting the implementation.
推荐答案
最简单的方法是使用股票代码:
Simplest way would be to use a Ticker:
ticker := time.NewTicker(time.Second * 1).C
go func() {
for {
select {
case <- ticker:
response,_ := http.Get("http://...")
_, err := io.Copy(os.Stdout, response.Body)
if err != nil {
log.Fatal(err)
}
response.Body.Close()
}
}
}()
time.Sleep(time.Second * 10)
这篇关于定期轮询Go中的REST端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文