英文:
Periodically polling a REST endpoint in Go
问题
我正在尝试编写一个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.
答案1
得分: 12
最简单的方法是使用 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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论