Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/mstryoda/gommons
- Owner: mstrYoda
- Created: 2022-12-01T18:10:50.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-07T12:37:37.000Z (over 1 year ago)
- Last Synced: 2024-10-16T06:13:23.215Z (22 days ago)
- Topics: cmd, concurrency, go, golang, gopher, library, unsafe, utils
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 73
- Watchers: 9
- Forks: 9
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
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 utilsAsync
#### 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