ytui/pubsub/ptexp/main.go

64 lines
905 B
Go
Raw Normal View History

2022-06-26 19:30:26 -04:00
package main
import (
"fmt"
"sync"
"time"
"gitlab.com/balki/ytui/pubsub"
)
func main() {
fmt.Println("vim-go")
foo()
// bar()
}
func foo() {
pt := pubsub.NewProgressTracker()
pc, err := pt.Publish("id1")
sc := pt.Subscribe("id1")
if err != nil {
panic(err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
fmt.Println("subscriber loop")
for msg := range sc {
fmt.Println("received", msg)
}
fmt.Println("subscriber loop done")
wg.Done()
}()
for i := 0; i < 10; i++ {
msg := fmt.Sprint("msg_", i)
// fmt.Println("sending: ", msg)
pc <- msg
}
close(pc)
wg.Wait()
fmt.Println("sleeping")
time.Sleep(10 * time.Second)
}
func bar() {
c := make(chan int, 100)
for i := 0; i < 10; i++ {
c <- i
}
close(c)
ok := true
var v int
for ok {
select {
case v, ok = <-c:
fmt.Println(v, ok)
default:
fmt.Println("in default")
}
}
fmt.Println("done")
}