option

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package option provides a set of options for the client.

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 applies a slice of functions to a value.

Example
package main

import (
	"fmt"

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

type Server struct {
	Name string
}

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

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

	option.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/option"
)

type Server struct {
	Name string
}

func WithNameE(p string) option.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 := option.ApplyE(
		s,
		WithNameE("localhost"),
	)

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

}
Output:
<nil>
localhost

Types

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