option

package
v0.296.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

option port

The option port is an idiomatic way to introduce optional arguments and default values to your functions using the variadic parameter syntax.

func MyFunctionWithOptions(arg1 string, arg2 int, opts ...Option) {
    conf := option.Use[config](opts)
    // ...
}

Documentation

Overview

Example
package main

import (
	"fmt"

	"go.llib.dev/frameless/port/option"
)

type config struct {
	Foo int
}

func (c *config) Init() {
	c.Foo = 42 // default value for Foo config
}

type Option interface {
	option.Option[config]
}

func FooIs(foo int) Option {
	return option.Func[config](func(c *config) {
		c.Foo = foo
	})
}

func FuncWithOptionalConfigurationInput(arg1 string, opts ...Option) string {
	conf := option.Use[config](opts)

	return fmt.Sprintf("Hello %s. (foo=%d)", arg1, conf.Foo)
}

func main() {
	fmt.Println(
		FuncWithOptionalConfigurationInput("argument", FooIs(42)),
	)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure[Config any](receiver Config, target *Config)

Configure is a default implementation that can be used to implement the Option interface' Configure method.

func Use

func Use[Config any, OPT Option[Config]](opts []OPT) Config

Types

type Func

type Func[Config any] func(*Config)

func (Func[Config]) Configure

func (fn Func[Config]) Configure(c *Config)

type Option

type Option[Config any] interface {
	Configure(*Config)
}

Jump to

Keyboard shortcuts

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