Categorygithub.com/yyle88/done
repositorypackage
1.0.18
Repository: https://github.com/yyle88/done.git
Documentation: pkg.go.dev

# README

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

Done - Simple Error Handling in Go

Done allows you to focus on your business logic without repetitive if err != nil patterns.

when you write logic:

if err := run(); err != nil{
    panic(err)
}

then you can use done:

done.Done(run())

CHINESE README

中文说明


Features

  • Simple error handling: Handle errors quickly with simple functions.
  • Supports multiple results: Works well with functions that return a value and an error type.
  • Less repeated code: Replace repeated error checks with short and clear functions.

Installation

go get github.com/yyle88/done

How It Works

The Done package wraps error checking into easy-to-use assertions, enabling you to handle errors while focusing on the success path of code.


Core Types

TypeDescription
Ve[V any]Holds a value and an error. Provides methods like Done, Must, and Soft.
Vpe[V any]For pointer values, includes methods such as Sure, Nice, and Good.
Vce[V comparable]For comparable values, provides methods like Same, Diff, and Equals.

Key Functions

FunctionDescription
DonePanics if an error exists.
MustEnsures the error is nil and returns the value.
SoftLogs a warning for errors but does not panic.
FataLogs the error at a "fatal" level and panics.

Functionality Summary

CategoryFunctionsDescription
Error HandlingDone, Must, SoftPanics on error or handles it in a specific way.
Result ValidationSure, Nice, SomeEnsures the result is not zero and returns it.
Result ValidationGood, Fine, SafeEnsures the result is not zero without returning it.
Zero Value CheckingZero, None, VoidEnsures the result is the zero value of its type.
Value ComparisonsSame, Diff, Is, EqualsChecks if values are the same, different, or match specific conditions.

Advanced Error Handling

UtilityDescription
VceFor comparable values, includes methods like Same, Diff, Is, and Equals.
VseFor string operations, includes methods like HasPrefix, HasSuffix, and Contains.
VneFor numeric operations, includes methods like Gt (greater than), Lt (less than), etc.

Example Usage

Standard Error Handling

package main

import (
	"fmt"
)

func main() {
	xyz, err := NewXyz()
	if err != nil {
		panic(err) // Handle errors manually
	}
	abc, err := xyz.Abc()
	if err != nil {
		panic(err)
	}
	uvw, err := abc.Uvw()
	if err != nil {
		panic(err)
	}
	fmt.Println(uvw.Message)
}

Using Done

package main

import (
	"fmt"
	"github.com/yyle88/done"
)

func main() {
	xyz := done.VCE(NewXyz()).Nice()
	abc := done.VCE(xyz.Abc()).Nice()
	uvw := done.VCE(abc.Uvw()).Nice()
	fmt.Println(uvw.Message)
}

This approach simplifies the code by chaining function calls with error handling assertions.


Chaining Operations

Without Done

package main

import (
	"fmt"
	"strconv"

	"github.com/pkg/errors"
)

func main() {
	stringNum, err := fetch()
	if err != nil {
		panic(err)
	}
	num, err := toInt(stringNum)
	if err != nil {
		panic(err)
	}
	if num <= 0 {
		panic(errors.New("num <= 0"))
	}
	fmt.Println(num)
}

With Done

package main

import (
	"fmt"
	"strconv"

	"github.com/yyle88/done"
)

func main() {
	num := done.VNE(toInt(done.VCE(fetch()).Nice())).Gt(0)
	fmt.Println(num)
}

By using Done, you eliminate repetitive error checks and enable inline assertions for conditions, resulting in cleaner and more maintainable code.


Conclusion

The Done package is a powerful tool for simplifying error handling, especially in informal or small-scale projects. It helps reduce boilerplate, makes error handling concise, and lets you focus on writing clean and efficient business logic.

Give it a try and share your feedback!


License

This project is licensed under the MIT License. See the LICENSE file for details.


Contribute and Support

Welcome to contribute to this project by submitting pull requests or reporting issues.

If you find this package helpful, give it a star on GitHub!

Thank you for your support!