hybridgroup.gobot/gobot.go

66 lines
940 B
Go
Raw Normal View History

2013-10-23 07:45:31 +08:00
package gobot
import (
2013-11-14 12:44:54 +08:00
"math/rand"
"net"
"time"
2013-10-23 07:45:31 +08:00
)
2013-10-27 07:41:43 +08:00
func Every(t string, f func()) {
2013-11-14 12:44:54 +08:00
dur := parseDuration(t)
go func() {
for {
time.Sleep(dur)
go f()
}
}()
2013-10-24 13:00:03 +08:00
}
2013-10-27 07:41:43 +08:00
func After(t string, f func()) {
2013-11-14 12:44:54 +08:00
dur := parseDuration(t)
go func() {
time.Sleep(dur)
f()
}()
2013-10-27 07:41:43 +08:00
}
2013-10-31 09:37:57 +08:00
func parseDuration(t string) time.Duration {
2013-11-14 12:44:54 +08:00
return ParseDuration(t)
2013-10-31 09:37:57 +08:00
}
func ParseDuration(t string) time.Duration {
2013-11-14 12:44:54 +08:00
dur, err := time.ParseDuration(t)
if err != nil {
panic(err)
}
return dur
2013-10-31 09:37:57 +08:00
}
2013-10-27 07:41:43 +08:00
func Random(min int, max int) int {
2013-11-14 12:44:54 +08:00
rand.Seed(time.Now().UTC().UnixNano())
return rand.Intn(max-min) + min
2013-10-29 11:26:05 +08:00
}
2013-11-14 12:44:54 +08:00
func On(cs chan interface{}) interface{} {
for s := range cs {
return s
}
return nil
2013-10-29 11:26:05 +08:00
}
2013-10-30 07:37:10 +08:00
func Work(robots []Robot) {
2013-11-14 12:44:54 +08:00
for s := range robots {
go robots[s].Start()
}
for {
time.Sleep(10 * time.Millisecond)
}
2013-10-30 07:37:10 +08:00
}
2013-10-31 09:37:57 +08:00
func ConnectTo(port string) net.Conn {
2013-11-14 12:44:54 +08:00
tcpPort, err := net.Dial("tcp", port)
if err != nil {
panic(err)
}
return tcpPort
2013-10-31 09:37:57 +08:00
}