Skip to content
go-options-gen logo

Go Options Gen

go-options-gen

Expert in generating functional options for Go structs using the options-gen library.

SKILL.md

Full skill instructions

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

  • File Naming:
    • Single Option Set: Struct definition MUST be in options.go, and generated code MUST be in options_generated.go.
    • Multiple Option Sets: For a component named MyService, the struct (e.g., MyServiceOptions) MUST be in myservice_options.go, and generated code MUST be in myservice_options_generated.go.
  • Encapsulation:
    • Options fields within the struct SHOULD be unexported (start with a lowercase letter) to prevent direct modification from outside the package.
  • Tooling:
    • Always run the tool using go tool options-gen.
    • Install and track the tool in go.mod using:
      go get -tool github.com/​kazhuravlev/​options-gen/​cmd/​options-gen@latest
      
  • Validation:
    • Always include validation tags (using go-playground/​validator syntax) for configuration fields.
    • ALWAYS call the generated Validate() method within the component's constructor.
  • Component Integration:
    • Store the resulting options struct in an unexported field named opts within your component struct.

Developer Workflow

  1. Installation: Ensure the tool is tracked in your project:

    go get -tool github.com/​kazhuravlev/​options-gen/​cmd/​options-gen@latest
    
  2. 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.

    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"`
    }
    
  3. Generate: Run the generator:

    go generate ./​options.go
    
  4. Integration: Use the generated types in your component's constructor and store them in an opts field.

    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

  • Use option:"mandatory" for fields that have no safe default (e.g., API keys, target URLs). These become required arguments in NewOptions().
  • Use default:"value" for sensible defaults. options-gen supports basic types and time.Duration.

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

  • Use validate:"required" for any field that must not be zero-valued.
  • Use validate:"oneof=tcp udp" for enum-like string fields.
  • Use validate:"min=1" for counters or sizes.

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

  • Examples: Complete implementations showing unexported fields, validation, and component integration can be found in the assets directory.

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View