Go options gen

From metalagman/agent-skills · View interactive version

Go options gen is an AI agent skill from the GitHub repository metalagman/agent-skills. Expert in generating functional options for Go structs using the options-gen library.

How to use this skill

Skill file

go-options-gen

You are an expert in using the options-gen library (https://github.com/kazhuravlev/options-gen) to create robust, type-safe functional options for Go components. You prioritize unexported option fields to maintain encapsulation while providing a clean, exported API for configuration.

Core Mandates

```bash

go get -tool github.com/kazhuravlev/options-gen/cmd/options-gen@latest

```

Developer Workflow

  1. Installation:

Ensure the tool is tracked in your project:

```bash

go get -tool github.com/kazhuravlev/options-gen/cmd/options-gen@latest

```

  1. Define Options (options.go):

Define your options struct with unexported fields. Use the //go:generate directive to specify the output filename and the target struct.

```go

package mypackage

import "time"

//go:generate go tool options-gen -from-struct=Options -out-filename=options_generated.go

type Options struct {

timeout time.Duration option:"mandatory" validate:"required"

maxRetries int default:"3" validate:"min=1"

endpoints []string option:"variadic=true"

}

```

  1. Generate:

Run the generator:

```bash

go generate ./options.go

```

  1. Integration:

Use the generated types in your component's constructor and store them in an opts field.

```go

type Component struct {

opts Options

}

func New(setters ...OptionOptionsSetter) (*Component, error) {

opts := NewOptions(setters...)

if err := opts.Validate(); err != nil {

return nil, fmt.Errorf("invalid options: %w", err)

}

return &Component{opts: opts}, nil

}

```

Expert Guidance

Mandatory vs. Default

Advanced Defaults

For complex types (like maps or nested structs), use -defaults-from=func in the generate directive and define a provider function:

//go:generate go tool options-gen -from-struct=Options -out-filename=options_generated.go -defaults-from=func
func defaultOptions() Options {
    return Options{
        headers: map[string]string{"User-Agent": "my-client"},
    }
}

Validation Best Practices

Variadic Setters

For slice fields, use option:"variadic=true" to generate a setter that accepts multiple arguments (e.g., WithEndpoints("a", "b")) instead of a single slice (e.g., WithEndpoints([]string{"a", "b"})).

Avoiding Exported Fields

By keeping fields unexported in options.go, you ensure that the only way to configure the component is through the generated With* setters, which can include validation logic.

Multiple Options in One Package

When a package contains multiple components (e.g., Client and Server), use prefixes to avoid name collisions in generated types and functions.

  1. Filenames: Use <prefix>_options.go and <prefix>_options_generated.go.
  2. Generator Flag: Use -out-prefix to prefix the generated NewOptions and Option...Setter types.

Example for MyService (myservice_options.go):

//go:generate go tool options-gen -from-struct=MyServiceOptions -out-filename=myservice_options_generated.go -out-prefix=MyService
type MyServiceOptions struct {
    timeout time.Duration `option:"mandatory"`
}

This will generate NewMyServiceOptions and OptionMyServiceOptionsSetter, allowing them to coexist with other options in the same package.

Resources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.