option

package
v0.300.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2025 License: Apache-2.0 Imports: 0 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.ToConfig[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.ToConfig[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 ToConfig added in v0.300.0

func ToConfig[Config any, Opt Option[Config]](opts []Opt) Config

func ToConfig[Config any, Opt Option[Config]](opts ...Opt) Config {

Types

type Func

type Func[Config any] func(*Config)

Func (option.Func[Config]) is a default implementation for creating options.

func (Func[Config]) Configure

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

type Option

type Option[Config any] interface {
	// Configure will configure an option.
	Configure(*Config)
}

Jump to

Keyboard shortcuts

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