deferhelper

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: MIT Imports: 1 Imported by: 1

README

deferhelper

Go Reference

Helpers for Go's defer statement.

Documentation

Overview

Package deferhelper contains defer-related helpers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeforeAfter

func BeforeAfter(before, after func()) func()

BeforeAfter, if called in the following manner from a defer statement at the very beginning of a function,

	func Example() {
	  defer deferhelper.BeforeAfter(before(), after())()
    // do something
	}

will call 'before' function before executing the function code and 'after' function after executing the function code.

Example
defer BeforeAfter(func() { fmt.Println("before") }, func() { fmt.Println("after") })()
fmt.Println("in the middle")
Output:
before
in the middle
after

func WriteBeforeAfter

func WriteBeforeAfter(w io.Writer, before, after []byte) func()

WriteBeforeAfter is like BeforeAfter but writes 'before' and 'after' slices to 'w' before and after executing the function code. It panics if a write to 'w' fails.

func WriteStringBeforeAfter

func WriteStringBeforeAfter(w io.Writer, before, after string) func()

WriteStringBeforeAfter is like WriteBeforeAfter but accepts strings. It panics if a write to 'w' fails.

Example
defer WriteStringBeforeAfter(os.Stdout, "", "")()
defer WriteStringBeforeAfter(os.Stdout, "only before\n", "")()
defer WriteStringBeforeAfter(os.Stdout, "before\n", "after\n")()
defer WriteStringBeforeAfter(os.Stdout, "", "only after\n")()
fmt.Println("in the middle")
Output:
only before
before
in the middle
only after
after

Types

This section is empty.

Jump to

Keyboard shortcuts

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