Skip to content

mstrYoda/gommons

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gommons

Swiss army knife for Golang developers

Features

  • 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

Async tasks

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

Async tasks with results

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

Run posix command and get output as byte array

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

Run posix command with pipes

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

str := String([]byte("test"))

Byte to string

byteArr := Byte("test")
Time utils

Function execution elapsed time utility

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

About

Swiss army knife for Golang developers

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages