https://github.com/nikhilsaraf/go-tools
A collection of tools for Golang
https://github.com/nikhilsaraf/go-tools
Last synced: 12 months ago
JSON representation
A collection of tools for Golang
- Host: GitHub
- URL: https://github.com/nikhilsaraf/go-tools
- Owner: nikhilsaraf
- License: mit
- Created: 2018-11-14T02:53:08.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-27T19:18:09.000Z (almost 7 years ago)
- Last Synced: 2024-07-31T20:51:53.556Z (over 1 year ago)
- Language: Go
- Size: 9.77 KB
- Stars: 15
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- go-awesome-with-star-updatetime - go-tools/multithreading - Manage a pool of goroutines using this lightweight library with a simple API. (Goroutines / Advanced Console UIs)
- awesome-Char - go-tools/multithreading - Manage a pool of goroutines using this lightweight library with a simple API. (Goroutines / Advanced Console UIs)
- awesome-go-info - go-tools
- awesome-go-with-stars - go-tools/multithreading - 03-27 | (Goroutines / Search and Analytic Databases)
- awesome-go - go-tools/multithreading - Manage a pool of goroutines using this lightweight library with a simple API. (Goroutines / Search and Analytic Databases)
- awesome-go-plus - go-tools/multithreading - Manage a pool of goroutines using this lightweight library with a simple API.  (Goroutines / Search and Analytic Databases)
- awesome-go - go-tools/multithreading - Manage a pool of goroutines using this lightweight library with a simple API. (Goroutines / Search and Analytic Databases)
- fucking-awesome-go - go-tools/multithreading - Manage a pool of goroutines using this lightweight library with a simple API. (Goroutines / Search and Analytic Databases)
- awesome-go-cn - go-tools/multithreading
- awesome-go - go-tools/multithreading - | - | - | (Goroutines / Advanced Console UIs)
- awesome-go - go-tools/multithreading - Manage a pool of goroutines using this lightweight library with a simple API. (Goroutines / Advanced Console UIs)
- awesome-go - go-tools/multithreading - Manage a pool of goroutines using this lightweight library with a simple API. (Goroutines / Search and Analytic Databases)
- awesome-go - go-tools/multithreading - Manage a pool of goroutines using this lightweight library with a simple API. (Goroutines / Search and Analytic Databases)
- awesome-go-extra - go-tools - 11-14T02:53:08Z|2019-03-27T19:18:09Z| (Goroutines / Advanced Console UIs)
- awesome-go - go-tools/multithreading - Manage a pool of goroutines using this lightweight library with a simple API. (Goroutines / Search and Analytic Databases)
- awesome-go-cn - go-tools/multithreading - tools) [![godoc][D]](https://godoc.org/github.com/nikhilsaraf/go-tools) (Goroutines / 检索及分析资料库)
- awesome-go-cn - go-tools/multithreading - tools) [![godoc][D]](https://godoc.org/github.com/nikhilsaraf/go-tools) (Goroutines / 检索及分析资料库)
- awesome-go - go-tools/multithreading - Manage a pool of goroutines using this lightweight library with a simple API. (Goroutines / Search and Analytic Databases)
README
# go-tools
A collection of tools for Golang, focusing on concurrency and goroutines
[](https://godoc.org/github.com/nikhilsaraf/go-tools)
[](https://goreportcard.com/report/github.com/nikhilsaraf/go-tools)
[](https://travis-ci.org/nikhilsaraf/go-tools)
[](https://raw.githubusercontent.com/nikhilsaraf/go-tools/master/LICENSE)
The [multithreading](multithreading) library currently supports a `ThreadTracker` struct that allows you to easily manage goroutines.
- Create new goroutines.
- Wait for all goroutines to finish.
- Set deferred functions to be executed after goroutines finish.
- Easily handle panics inside goroutines with a panic handler.
- Stop the `threadTracker` from receiving new functions.
- Fetch the number of currently active goroutines.
## Install
Install the package with:
```bash
go get github.com/nikhilsaraf/go-tools/multithreading
```
Import it with:
```go
import "github.com/nikhilsaraf/go-tools/multithreading"
```
and use `multithreading` as the package name inside the code.
## Example
```go
package main
import (
"fmt"
"github.com/nikhilsaraf/go-tools/multithreading"
)
func main() {
// create thread tracker instance
threadTracker := multithreading.MakeThreadTracker()
// start thread functions
for i := 0; i < 10; i++ {
err := threadTracker.TriggerGoroutine(func(inputs []interface{}) {
// pass `i` as a value to the goroutine and read from `inputs`.
// this is needed to "bind" the variable to this goroutine.
value := inputs[0].(int)
fmt.Printf("Goroutine #%d\n", value)
}, []interface{}{i})
if err != nil {
panic(err)
}
}
// wait for all threads to finish
threadTracker.Wait()
fmt.Printf("done\n")
}
```
Sample Output:
```
Goroutine #1
Goroutine #2
Goroutine #9
Goroutine #0
Goroutine #3
Goroutine #7
Goroutine #6
Goroutine #4
Goroutine #8
Goroutine #5
done
```
## Test Examples
[thread_tracker_test.go](/multithreading/thread_tracker_test.go)