funcopt

package
v3.0.0-rc44 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: Apache-2.0 Imports: 0 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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