定期在Go中轮询REST端点

huangapple go评论69阅读模式
英文:

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 &lt;- ticker:
			response,_ := http.Get(&quot;http://...&quot;)
			_, err := io.Copy(os.Stdout, response.Body)
			if err != nil {
				log.Fatal(err)
			}
			response.Body.Close()
		}
	}

}()


time.Sleep(time.Second * 10)

huangapple
  • 本文由 发表于 2016年9月7日 15:30:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/39363794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定