rg

package module
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2024 License: MIT Imports: 2 Imported by: 50

README

rg - Royal Guard

Go Reference Go

A generics-based throw-catch approach in Go

中文文档

Usage

Throw

Any function with the latest return value of type error can be wrapped by rg.Must (or rg.Must2, rg.Must3 ...)

Catch

Use defer rg.Guard(&err) to catch the thrown error, and the error will be assigned to err if any.

Example

package demo

import (
	"context"
	"encoding/json"
	"gopkg.in/yaml.v3"
	"github.com/yankeguo/rg"
	"os"
)

// jsonFileToYAMLUgly this is a demo function WITHOUT rg
func jsonFileToYAMLUgly(filename string) (err error) {
	var buf []byte
	if buf, err = os.ReadFile(filename); err != nil {
		return
	}
	var m map[string]interface{}
	if err = json.Unmarshal(buf, &m); err != nil {
		return
	}
	if buf, err = yaml.Marshal(m); err != nil {
		return
	}
	buf = rg.Must(yaml.Marshal(m))
	if err = os.WriteFile(filename+".yaml", buf, 0640); err != nil {
		return
	}
	return
}

// jsonFileToYAML this is a demo function WITH rg
func jsonFileToYAML(filename string) (err error) {
	defer rg.Guard(&err)
	buf := rg.Must(os.ReadFile(filename))
	var m map[string]interface{}
	rg.Must0(json.Unmarshal(buf, &m))
	buf = rg.Must(yaml.Marshal(m))
	rg.Must0(os.WriteFile(filename+".yaml", buf, 0640))
	return
}

func GuardCallbackOnErrWithContext(ctx context.Context)(err error) {
	rg.OnGuardWithContext = func(ctx context.Context, r any) {
        // do something like logging with ctx on guarded
	}
	// for recovery panic
	defer rg.Guard(&err, rg.WithContext(ctx))
	// if err is not nil, it will panic
	file:=rg.Must(os.ReadFile("something.txt"))
	rg.Must0(os.WriteFile("something.txt", file, 0640))
	return
}

Credits

GUO YANKE, MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var OnGuard func(r any)

OnGuard is a global hook for Guard Deprecated use OnGuardWithContext

View Source
var OnGuardWithContext func(ctx context.Context, r any)

OnGuardWithContext is a global hook for Guard And GuardWithContext

Functions

func Guard

func Guard(err *error, opts ...Option)

Guard recover from panic and set err

func Must

func Must[T any](v T, err error) T

Must panic err if not nil, else return remaining values

func Must0

func Must0(err error)

Must0 panic err if not nil

func Must2

func Must2[T1 any, T2 any](v1 T1, v2 T2, err error) (T1, T2)

Must2 panic err if not nil, else return remaining values

func Must3

func Must3[T1 any, T2 any, T3 any](v1 T1, v2 T2, v3 T3, err error) (T1, T2, T3)

Must3 panic err if not nil, else return remaining values

func Must4

func Must4[T1 any, T2 any, T3 any, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) (T1, T2, T3, T4)

Must4 panic err if not nil, else return remaining values

func Must5

func Must5[T1 any, T2 any, T3 any, T4 any, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, err error) (T1, T2, T3, T4, T5)

Must5 panic err if not nil, else return remaining values

func Must6

func Must6[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, err error) (T1, T2, T3, T4, T5, T6)

Must6 panic err if not nil, else return remaining values

func Must7

func Must7[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, err error) (T1, T2, T3, T4, T5, T6, T7)

Must7 panic err if not nil, else return remaining values

Types

type Option added in v1.3.0

type Option func(opts *options)

Option for Guard

func WithContext added in v1.3.0

func WithContext(ctx context.Context) Option

WithContext set context for Guard

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL