mainrun

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2023 License: MIT Imports: 9 Imported by: 1

README

go-mainrun

Go Reference

Utility for main package

How to use

func main() { mainrun.Run(run) }

func run(ctx context.Context) error {

  // ...

  return nil
}

The ctx passed to run will be cancelled if the program caught os signal (graceful shutdown).

The returned error will be printed to stderr and the program will be exit with exit code 1

Documentation

Overview

Package mainrun.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go added in v1.1.0

func Go(f func() error) <-chan error

Go run the f function in new go routine, and return chan to get the value returned by f

func Go2 added in v1.1.0

func Go2[Result any](f func() (Result, error)) <-chan Go2Result[Result]

similar with Go but returning some value instead of just error

func Interrupted added in v1.2.0

func Interrupted(ctx context.Context) os.Signal

Return nil if graceful shutdown is not requested yet, otherwise return the signal

func OnError

func OnError(f func(error) int)

When function that passed to Run is returned error or panic, run f, the returned int will be used to os.Exit function.

func Run

func Run(f func(ctx context.Context) error)

Run f

The function Run never return.

ctx passed to f will be canceled when graceful shutdown is requested, if f returned error or panic, then log it and run os.Exit(1), otherwise run os.Exit(0).

Types

type Go2Result added in v1.1.0

type Go2Result[Result any] struct {
	Result Result
	Error  error
}

Result of Go2

Jump to

Keyboard shortcuts

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