Giter Site home page Giter Site logo

go-tools's Introduction

Go Tools

Collection of utility struct and functions

Require go v1.19

Arrays

1. ForEach

Loop an array

arr := []int{10, 20, 30}
gotools.ForEach(arr, func(row, i int) {
    fmt.Printf("(%d, %d)", i, row)
})

// prints (0,10) (1,20) (2,30)

2. Map

Loop an array and map each entry into a new value, return a new array with the same size

arr := []int{10, 20, 30}
mapped := gotools.Map(arr, func(row, i int) int {
    return row + i + 10
})

// mapped = [20, 31, 42]

3. Reduce

Reduce an array into a new value, based on its initial value

arr := []int{10, 20, 30}
reduced := gotools.Reduce(
    arr,
    // sum all rows
    func(acc, row, i int) int {
        return acc + row
    },
    0, // initial value
)

// reduced = 60

4. Find

Find value and index in an array based on given predicate

arr := []int{10, 20, 30}
found, index := gotools.Find(
    arr,
    func(row int) bool {
        return row == 20
    },
)

// found = 20, index = 1

Returns zero value and index = -1 if not found

arr := []int{10, 20, 30}
found, index := gotools.Find(
    arr,
    func(row int) bool {
        return row == 1000
    },
)

// found = 0, index = -1

5. Filter

Find all values in an array based on given predicate. Returns empty array if nothing is found

arr := []int{10, 20, 30}
found := gotools.Filter(
    arr,
    func(row int) bool {
        return row > 15
    },
)

// found = [20, 30]

6. Any

Check whether array contains given predicate.

arr := []int{10, 20, 30}
exists := gotools.Any(
    arr,
    func(row int) bool {
        return row > 15
    },
)

// exists = true

7. All

Check whether all entries in the array matches given predicate

arr := []int{10, 20, 30}
all := gotools.All(
    arr,
    func(row int) bool {
        return row > 5
    },
)

// all = true

8. Chunk

Split an array into smaller chunks based on given chunkSize

arr := []int{10, 20, 30, 40, 50}
chunks := gotools.Chunk(arr, 3)

// chunks = [[10 20 30], [40 50]]

Concurrencies

1. Runnable

Runnable job that utilize channel and go routines to spawn multiple worker

runnable := gotools.NewRunnable(
    10, // number of workers
    func(id int, this *Runnable[int], message int) {
        time.Sleep(1000 * time.Millisecond)
        fmt.Printf("id: %d, message: %d\n", id, message)
    },
)
runnable.Run() // Spawn the worker

// Publish messages
go runnable.Publish(1)
go runnable.Publish(2)
go runnable.Publish(3)
go runnable.Publish(4)
go runnable.Publish(5)
go runnable.Publish(6)
go runnable.Publish(7)
go runnable.Publish(8)
go runnable.Publish(9)
go runnable.Publish(10)

// Wait for all message to finish processing by worker
runnable.Wait()

Utilities

1. If

Lazy ternary operator :P

res := gotools.If(1 == 1, "one", "not one")
fmt.Println(res)

// prints one

go-tools's People

Contributors

bastianrob avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.