funcopt

package
v0.0.0-...-ffd2651 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Overview

Package funcopt is a functional options helper package.

Functional options benefits are described at https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis

A typical allocator is implemented as:

func New(opts ...funcopt.O) *T {
    t := &T{<my defaults>}
    funcopt.Apply(t, opts...)
    return t
}

Example:

func WithName(name string) funcopt.O {
    return funcopt.F(func(i interface{}) error {
        t := i.(*T)
        t.name = name
        return nil
    })
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(t interface{}, opts ...O) error

Apply loops over the functional options and executes their setter function.

Types

type F

type F func(interface{}) error

F is the prototype of the setter function returned by the option function.

type O

type O interface {
	// contains filtered or unexported methods
}

O is the interface that a functional option must implement.

Jump to

Keyboard shortcuts

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