deferhelper

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2025 License: MIT Imports: 3 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.

func WriteStringBeforeAfter

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

WriteStringBeforeAfter is like BeforeAfter but writes 'before' and 'after' strings to 'w' before and after executing the function code.

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

before
in the middle
after

Types

This section is empty.

Jump to

Keyboard shortcuts

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