Skip to content
/ done Public

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

License

Notifications You must be signed in to change notification settings

yyle88/done

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Type Description
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

Function Description
Done Panics if an error exists.
Must Ensures the error is nil and returns the value.
Soft Logs a warning for errors but does not panic.
Fata Logs the error at a "fatal" level and panics.

Functionality Summary

Category Functions Description
Error Handling Done, Must, Soft Panics on error or handles it in a specific way.
Result Validation Sure, Nice, Some Ensures the result is not zero and returns it.
Result Validation Good, Fine, Safe Ensures the result is not zero without returning it.
Zero Value Checking Zero, None, Void Ensures the result is the zero value of its type.
Value Comparisons Same, Diff, Is, Equals Checks if values are the same, different, or match specific conditions.

Advanced Error Handling

Utility Description
Vce For comparable values, includes methods like Same, Diff, Is, and Equals.
Vse For string operations, includes methods like HasPrefix, HasSuffix, and Contains.
Vne For 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!