goreturn
is a Go library that provides a set of functions to run a function in a goroutine and return its result(s) in a channel. This library is designed to make it easier to work with goroutines and channels in Go, especially when dealing with functions that return multiple values.
Return0(fn func()) <-chan any
: Runs a function with no return values in a goroutine and returns a channel that will receive a singlenil
value when the function completes.Return1[T1 any](fn func() T1) <-chan T1
: Runs a function with one return value in a goroutine and returns a channel that will receive the return value.Return2[T1 any, T2 any](fn func() (T1, T2)) <-chan *Tuple[T1, T2]
: Runs a function with two return values in a goroutine and returns a channel that will receive a tuple containing the return values.Return3[T1 any, T2 any, T3 any](fn func() (T1, T2, T3)) <-chan *Tuple3[T1, T2, T3]
: Runs a function with three return values in a goroutine and returns a channel that will receive a tuple containing the return values.
To install goreturn
, use the go get
command:
go get github.com/Eyal-Shalev/goreturn
Here is an example of how to use goreturn
:
package main
import (
"fmt"
"github.com/Eyal-Shalev/goreturn"
)
func main() {
c := goreturn.Return1(func() int {
return 42
})
v := <-c
fmt.Println("Return1() returned", v)
}
In this example, the function func() int { return 42 }
is run in a goroutine, and its return value is received from the channel returned by Return1
.
Contributions to goreturn
are welcome! Please submit a pull request on GitHub.
goreturn
is licensed under the MIT License. See the LICENSE
file for details.