rese simplifies Go error handling and result extraction for multi-value function calls. It combines error and result checks into a single operation.
Example, the common way to handle errors in Go is like this:
num, err := run()
if err != nil {
panic(err)
}
ans, err := run2()
if err != nil {
panic(err)
}
res := num + ans
fmt.Println(res)
Simple(Use rese package):
res := rese.V1(run()) + rese.V1(run2())
fmt.Println(res)
rese stands for res (result) + err (error).
package main
import (
"fmt"
"github.com/yyle88/rese"
)
func run() (string, error) {
return "Hello, World!", nil
}
func main() {
// Using V1 to check for error and get the result
result := rese.V1(run())
fmt.Println(result) // Outputs: Hello, World!
}
package main
import (
"fmt"
"github.com/yyle88/rese"
)
type SomeType struct {
Rank int64
}
func getSomething() (*SomeType, error) {
v := &SomeType{Rank: 42}
return &v, nil
}
func main() {
// Using P1 to check error and ensure non-nil pointer
ptr := rese.P1(getSomething())
fmt.Println(ptr.Rank) // Outputs: 42
}
package main
import (
"fmt"
"github.com/yyle88/rese"
)
func getInt() (int, error) {
return 20, nil
}
func main() {
// Using C1 to check error and ensure non-zero result
num := rese.C1(getInt())
fmt.Println("Received:", num) // Outputs: 20
}
go get github.com/yyle88/rese
Function | Purpose | Returns |
---|---|---|
V0(err error) |
Checks the error and panics if it's not nil . No return value. |
None |
V1[T1 any](v1 T1, err error) T1 |
Checks the error, and if no error, returns v1 . |
Returns the value of type T1 |
V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2) |
Checks the error, and if no error, returns v1 and v2 . |
Returns v1 of type T1 and v2 of type T2 |
P0(err error) |
Checks the error and panics if it's not nil . No return value. |
None |
P1[T1 any](v1 *T1, err error) *T1 |
Checks the error, checks that v1 is non-nil , and returns v1 . |
Returns a pointer to v1 of type T1 |
P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2) |
Checks the error, checks that v1 and v2 are non-nil , and returns v1 and v2 . |
Returns pointers to v1 and v2 of types T1 and T2 |
C0(err error) |
Checks the error and panics if it's not nil . No return value. |
None |
C1[T1 comparable](v1 T1, err error) T1 |
Checks the error, checks that v1 is not a zero value, and returns v1 . |
Returns v1 of type T1 |
C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2) |
Checks the error, checks that v1 and v2 are not zero values, and returns v1 and v2 . |
Returns v1 of type T1 and v2 of type T2 |
MIT License. See LICENSE.
Contributions are welcome! To contribute:
- Fork the repo on GitHub (using the webpage interface).
- Clone the forked project (
git clone https://github.com/yourname/repo-name.git
). - Navigate to the cloned project (
cd repo-name
) - Create a feature branch (
git checkout -b feature/xxx
). - Stage changes (
git add .
) - Commit changes (
git commit -m "Add feature xxx"
). - Push to the branch (
git push origin feature/xxx
). - Open a pull request on GitHub (on the GitHub webpage).
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project by submitting pull requests and reporting issues.
If you find this package valuable, give me some stars on GitHub! Thank you!!!
Thank you for your support!
Happy Coding with this package! 🎉
Give me stars. Thank you!!!