Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mstryoda/gommons

Swiss army knife for Golang developers
https://github.com/mstryoda/gommons

cmd concurrency go golang gopher library unsafe utils

Last synced: 11 days ago
JSON representation

Swiss army knife for Golang developers

Awesome Lists containing this project

README

        

# gommons

Swiss army knife for Golang developers

## Features

- [X] Async tasks
- [X] Async tasks with results
- [X] Async Pub/Sub Queue
- [X] Command exec utils
- [X] Zero alloc string-byte conversion
- [X] Function execution elapsed time util
- [ ] Time utils
- [ ] Array utils

Async

#### Async tasks

```go
New().Task(
func () {
a = 1
fmt.Println("1")
}, func () {
b = 1
fmt.Println("2")
}).Await()
```

#### Async tasks with results

```go
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.

```go
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:

```go
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

#### Run posix command and get output as byte array

```go
out := Exec("echo", "test")
```

#### Run posix command with pipes

```go
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

#### String to byte array zero allocation

```go
str := String([]byte("test"))
```
#### Byte to string

```go
byteArr := Byte("test")
```

Time utils

#### Function execution elapsed time utility

```go
elapsedTime := ElapsedTime(func() {
time.Sleep(100 * time.Millisecond)
})
```

## Getting All Dependencies
### To get all Dependencies in project run `go mod tidy` in root of project