options

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 1 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 options implements the generic functional options pattern: Option and OptionE are functions that configure a value of type T, Apply and ApplyE run a slice of them against a value, and Builder and BuilderE collect options for reuse across multiple Apply calls.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[T any](v T, opts ...Option[T])

Apply calls each non-nil opt with v, applying every option in order.

Example
package main

import (
	"fmt"

	"github.com/foomo/go/options"
)

type Server struct {
	Name string
}

func WithName(p string) options.Option[*Server] {
	return func(s *Server) {
		s.Name = p
	}
}

func main() {
	s := &Server{}

	options.Apply(
		s,
		WithName("localhost"),
	)

	fmt.Printf("%s\n", s.Name)

}
Output:
localhost

func ApplyE

func ApplyE[T any](v T, opts ...OptionE[T]) error

ApplyE applies a slice of error-returning functions to a value and stops on the first encountered error.

Example
package main

import (
	"errors"
	"fmt"

	"github.com/foomo/go/options"
)

type Server struct {
	Name string
}

func WithNameE(p string) options.OptionE[*Server] {
	return func(s *Server) error {
		if len(p) == 0 {
			return errors.New("invalid name")
		}

		s.Name = p

		return nil
	}
}

func main() {
	s := &Server{}

	err := options.ApplyE(
		s,
		WithNameE("localhost"),
	)

	fmt.Println(err)
	fmt.Println(s.Name)

}
Output:
<nil>
localhost

func Build

func Build[T any](v T, builders ...interface{ List() []Option[T] })

Build applies every Option collected by each non-nil builder to v, in order.

func BuildE

func BuildE[T any](v T, builders ...interface{ List() []OptionE[T] }) error

BuildE applies a slice of error-returning functions to a value and stops on the first encountered error.

Types

type Builder

type Builder[T any] struct {
	Opts []Option[T]
}

Builder is a generic option builder that collects functional options.

Example
package main

import (
	"fmt"

	"github.com/foomo/go/options"
)

type MyOptions struct {
	Name string
}

type MyOptionsBuilder struct {
	options.Builder[*MyOptions]
}

func NewMyOptionsBuilder() *MyOptionsBuilder {
	return &MyOptionsBuilder{
		options.Builder[*MyOptions]{},
	}
}

func (b *MyOptionsBuilder) WithName(name string) *MyOptionsBuilder {
	b.Opts = append(b.Opts, func(o *MyOptions) {
		o.Name = name
	})

	return b
}

func main() {
	b := NewMyOptionsBuilder()
	b.WithName("example")

	o := MyOptions{}
	options.Build(&o, b)

	fmt.Println(o)

}
Output:
{example}

func (*Builder[T]) List

func (b *Builder[T]) List() []Option[T]

List returns the options collected in b.Opts.

type BuilderE

type BuilderE[T any] struct {
	Opts []OptionE[T]
}

BuilderE is a generic option builder that collects functional error-returning options.

Example
package main

import (
	"fmt"

	"github.com/foomo/go/options"
)

type MyOptions struct {
	Name string
}

type MyOptionsBuilderE struct {
	options.BuilderE[*MyOptions]
}

func MyBuilderE() *MyOptionsBuilderE {
	return &MyOptionsBuilderE{
		options.BuilderE[*MyOptions]{},
	}
}

func (b *MyOptionsBuilderE) WithName(name string) *MyOptionsBuilderE {
	b.Opts = append(b.Opts, func(o *MyOptions) error {
		o.Name = name
		return nil
	})

	return b
}

func main() {
	b := MyBuilderE()
	b.WithName("example")

	o := MyOptions{}
	if err := options.BuildE(&o, b); err != nil {
		panic(err)
	}

	fmt.Println(o)

}
Output:
{example}

func (*BuilderE[T]) List

func (b *BuilderE[T]) List() []OptionE[T]

List returns the options collected in b.Opts.

type Option

type Option[T any] func(T)

Option is a functional option.

type OptionE

type OptionE[T any] func(T) error

OptionE is a functional error-returning option.

Jump to

Keyboard shortcuts

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