Skip to content

Instantly share code, notes, and snippets.

@pallat
Last active October 24, 2017 03:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pallat/c5430c69fc77ccb9a3ff65ecbbfbd219 to your computer and use it in GitHub Desktop.
Save pallat/c5430c69fc77ccb9a3ff65ecbbfbd219 to your computer and use it in GitHub Desktop.
example queue usage
package main
import (
"fmt"
"github.com/pallat/queue"
)
type lot struct {
items []int
count chan struct{}
notify chan struct{}
}
func (l *lot) do(i <-chan int) {
for {
fmt.Println(l.items[<-i])
l.count <- struct{}{}
if len(l.count) == len(l.items) {
l.notify <- struct{}{}
return
}
}
}
func (l *lot) end() <-chan struct{} {
return l.notify
}
func main() {
l := lot{
count: make(chan struct{}, 10),
items: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
notify: make(chan struct{}),
}
q := queue.NewQueue(len(l.items))
go l.do(q.Pop())
go l.do(q.Pop())
go l.do(q.Pop())
go l.do(q.Pop())
<-l.end()
fmt.Println("total:", len(l.count))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment