https://github.com/rosemound/opts
Too simple, zero-dependencies library to flexible passing of dynamic params into variadic functions.
https://github.com/rosemound/opts
1ib dynamic-params go golang golib options opts utils variadic-functions
Last synced: 5 months ago
JSON representation
Too simple, zero-dependencies library to flexible passing of dynamic params into variadic functions.
- Host: GitHub
- URL: https://github.com/rosemound/opts
- Owner: Rosemound
- License: mit
- Created: 2025-10-08T18:58:31.000Z (9 months ago)
- Default Branch: dev
- Last Pushed: 2025-12-25T13:06:21.000Z (6 months ago)
- Last Synced: 2025-12-27T00:21:19.195Z (6 months ago)
- Topics: 1ib, dynamic-params, go, golang, golib, options, opts, utils, variadic-functions
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
OPTS
Too simple, zero-dependencies library to flexible passing dynamic options
[](https://github.com/rosemound/opts/actions/workflows/build.yml)
[](https://pkg.go.dev/github.com/rosemound/opts)
[](https://goreportcard.com/report/github.com/rosemound/opts)
### About
Too simple, zero-dependencies library to flexible passing of dynamic params into variadic functions.
### Installation
```bash
go get github.com/rosemound/opts/v2
```
### Usage mapped opts
```go
import (
"context"
"errors"
"github.com/rosemound/opts/v2"
)
// Mapped option key type
type OptionKey string
// Owner key
const OwnerKey OptionKey = "owner"
// Company key (like ctx keys)
const CompanyKey OptionKey = "company"
// simple service
type service struct {
owner string
company any
}
// main
func main() {
// service instantination with dynamic params
s, err := NewService(context.Background(), WithCompany("test"), WithOwner("test"))
// err handling
if err != nil {
panic(err)
}
// do staff
}
func NewService(ctx context.Context, o ...opts.Option[OptionKey]) (*service, error){
// init option container with err handling
c, err := opts.CreateContainerWithOptions(o)
if err != nil {
return nil, err
}
// or silently
c := opts.CreateContainerWithOptionsS(o)
// allocate your instance
return &service{
owner: conf.Get(OwnerKey).(string),
company: conf.Get(CompanyKey),
}, nil
}
// With company
func WithCompany(company any) opts.Option[OptionKey] {
return func(o opts.OptionContainer[OptionKey]) error {
o.Set(CompanyKey, company)
return nil
}
}
// With owner & err
func WithOwner(val string) opts.Option[OptionKey] {
var err error
if val == "" {
err = errors.New("owner must be present")
}
return func(o opts.OptionContainer[OptionKey]) error {
if err == nil {
o.Set(OwnerKey, val)
}
return err
}
}
```