Swiss army knife for Golang developers
- Async tasks
- Async tasks with results
- Async Pub/Sub Queue
- Command exec utils
- Zero alloc string-byte conversion
- Function execution elapsed time util
- Time utils
- Array utils
Async
New().Task(
func () {
a = 1
fmt.Println("1")
}, func () {
b = 1
fmt.Println("2")
}).Await()
results := NewAsyncWorkWithResult[int]().TaskWithResult(
func() int {
return 5
}, func() int {
return 11
}).AwaitResult()
Queue Pub/Sub example
Queue acts as a non-blocking message queue backing with unbuffered channel. Publish/Subscribe functions are not blocks code execution.
q := NewQueue[int]()
q.Publish(context.Background(), 1)
q.Publish(context.Background(), 2)
q.Subscribe(context.Background(), func(data int) {
fmt.Println("data readed ", data)
})
<-make(chan struct{})
You can also give timeout to both message publish and subscribe functions:
q := NewQueue[int]()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
q.Publish(context.Background(), 1)
q.Publish(ctx, 2)
q.Subscribe(ctx, func(data int) {
fmt.Println("data readed ", data)
})
<-make(chan struct{})
Command exec
out := Exec("echo", "test")
strReader := strings.NewReader("hello world")
outWriter := bytes.NewBuffer(nil)
errWriter := bytes.NewBuffer(nil)
ExecPipe(strReader, outWriter, errWriter, "echo", "test")
outputStr := outWriter.String()
Zero alloc string byte conversion
str := String([]byte("test"))
byteArr := Byte("test")
Time utils
elapsedTime := ElapsedTime(func() {
time.Sleep(100 * time.Millisecond)
})