tebata

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2025 License: MIT Imports: 6 Imported by: 7

README

Tebata(手旗)

GoDoc MIT License

logo

Overview

Simple linux signal handler for Go

Installation

go get -u github.com/syossan27/tebata

Usage

package main
 
import (
	"fmt"
	"strconv"
	"syscall"
	
	"github.com/syossan27/tebata"
)
 
func main() {
	t := tebata.New(syscall.SIGINT, syscall.SIGTERM)
	
	// Do function when catch signal.
	t.Reserve(sum, 1, 2)
	t.Reserve(hello)
	t.Reserve(os.Exit, 0)
	
	for {
		// Do something
	}
}
 
func sum(firstArg, secondArg int) {
	fmt.Println(strconv.Itoa(firstArg + secondArg))	
}
 
func hello() {
	fmt.Println("Hello")	
}
 
// Expect output when type Ctrl + C:
//   3
//   Hello

Why?

Look this article.

How to implement a signal handler in Go

Artwork credits

The Go gopher was designed by Renée French.
The gopher side portrait is designed by Takuya Ueda.
Licensed under the "Creative CommonsAttribution 3.0" license.

Documentation

Overview

Package tebata provides a way to handle OS signals gracefully.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidArgs = errors.New("invalid args argument: expected a slice")

ErrInvalidArgs is returned when invalid arguments are passed to Reserve.

View Source
var ErrInvalidFunction = errors.New("invalid function argument: expected a function")

ErrInvalidFunction is returned when a non-function is passed to Reserve.

View Source
var ErrTooFewArgs = errors.New("too few arguments: not enough arguments provided for function")

ErrTooFewArgs is returned when too few arguments are provided for the function.

View Source
var ErrTooManyArgs = errors.New("too many arguments: too many arguments provided for function")

ErrTooManyArgs is returned when too many arguments are provided for the function.

View Source
var ErrTypeMismatch = errors.New("type mismatch: function parameter types don't match provided argument types")

ErrTypeMismatch is returned when the function arguments don't match the provided arguments.

Functions

This section is empty.

Types

type Tebata

type Tebata struct {
	// contains filtered or unexported fields
}

Tebata handles signal-triggered function execution.

func New

func New(signals ...os.Signal) *Tebata

New creates a new Tebata instance and starts listening for the specified signals. It uses context for better cancellation support.

func (*Tebata) Close

func (s *Tebata) Close()

Close stops the signal handling and cleans up resources.

func (*Tebata) Reserve

func (s *Tebata) Reserve(function any, args ...any) error

Reserve registers a function to be executed when a signal is received. It returns an error if the function or arguments are invalid. It also validates that the provided arguments match the function's parameter types.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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