Archived
1
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues or pull requests.
fwzookeeper/vendor/github.com/kirillDanshin/myutils/syncPrint.go

33 lines
584 B
Go
Raw Normal View History

2018-12-22 17:38:56 +04:00
package myutils
import "fmt"
// SyncPrinter is a thread-safe printer.
// It will output given queue without
type SyncPrinter struct {
Queue chan string
Close chan bool
}
// NewSyncPrinter initialize a new SyncPrinter
func NewSyncPrinter() (*SyncPrinter, error) {
return &SyncPrinter{
Queue: make(chan string, 64),
Close: make(chan bool, 1),
}, nil
}
// Run a SyncPrinter instance
func (printer *SyncPrinter) Run() {
for {
select {
case s := <-printer.Queue:
fmt.Println(s)
case <-printer.Close:
close(printer.Close)
close(printer.Queue)
return
}
}
}