Gotimer
timer(wheel/heap) implement for go
Install / Use
/learn @gocpp/GotimerREADME
gotimer
examples:
package main
import (
"fmt"
"time"
"github.com/gocpp/gotimer/timeheap"
)
// timer callback
func selectHandler(id uint32) {
fmt.Printf("timerId:%d now:%v\n", id, time.Now())
}
func main() {
th := timeheap.New(selectHandler) // time heap
th.Start() // start
// add timer
th.AddTimer(1, 1*time.Second, false)
// wait
time.Sleep(2 * time.Second)
}
package main
import (
"fmt"
"time"
"github.com/gocpp/gotimer/timeheap"
)
type TimerCb interface {
Start()
Stop()
AddTimer(uint32, time.Duration, bool)
RemoveTimer(uint32)
}
// timer callback
func selectHandler(id uint32) {
fmt.Printf("timerId:%d now:%v\n", id, time.Now())
}
func stop(th TimerCb) {
for i := uint32(1); i <= 5; i++ {
time.Sleep(1 * time.Second)
th.RemoveTimer(i)
}
}
func main() {
var th TimerCb
th = timeheap.New(selectHandler) // time heap
th.Start() // start
// add timer for loop
for i := uint32(1); i <= 5; i++ {
th.AddTimer(i, 1*time.Second, true)
}
th.AddTimer(6, 6*time.Second, false)
go stop(th) // stop timer
select {}
}
Related Skills
node-connect
351.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
