This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("FMT" " Time") Func main () {requests:= Make (chanint,5) forI: =1; I <=2; i++{Requests<-i} close (requests) limiter:= time. Tick (Time.millisecond * $) forReq: =Range Requests {//Will loop two times and two values are sent in front of the requests channel<-limiter//execution here, it takes 200 milliseconds to continue execution, time. The FMT is defined above the Tick (timer). Println ("Request", req, time. Now ())} Burstylimiter:= Make (chan time. Time,3) forI: =0; I <3; i++{Burstylimiter<-Time . Now ()//Here Burstylimiter Channel continuously sends three values} go func () { forT: = range time. Tick (time. Second *2) {Burstylimiter<-t//here toward the Burstylimiter channel loop to send time. Tick}} () Burstyrequests:= Make (chanint,5) forI: =1; I <=5; i++{burstyrequests<-I<-Burstylimiter//First three times no speed limit, will print directly out the contents of the println later FMT. Println ("Request", I, time. Now ())} close (burstyrequests) forReq: =Range Burstyrequests {<-Burstylimiter//continues to receive burstylimiter values, except for the first three times, followed by time. Tick carries a speed limit of 2 seconds until the end of the FMT cycle. Println ("Request", req, time. Now ())}}
Note, put time. Tick (time. Second) will make it easier to see the effect by changing the value in seconds