0Pricing
Go Academy · Lesson

Option Functions

Configure with functions.

Option Functions is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Configure with Functions

The core idea: represent each optional setting as a function that mutates a config. These are the "option functions".

The Option Type

Define an Option as a function taking a pointer to the config struct.

package main

import "fmt"

type Config struct {
	Port    int
	Timeout int
}

type Option func(*Config)

func main() {
	var o Option
	fmt.Println(o == nil)
}

Writing an Option

An option returns a closure that sets one field. WithPort(9000) produces a function that assigns Port = 9000.

package main

import "fmt"

type Config struct{ Port int }
type Option func(*Config)

func WithPort(p int) Option {
	return func(c *Config) { c.Port = p }
}

func main() {
	c := &Config{}
	WithPort(9000)(c)
	fmt.Println(c.Port)
}

Closures Capture the Value

The returned function closes over the argument. WithPort(9000) remembers 9000 until applied later.

Multiple Options

Each setting gets its own option function. They all share the Option type.

package main

import "fmt"

type Config struct {
	Port    int
	Timeout int
}
type Option func(*Config)

func WithPort(p int) Option    { return func(c *Config) { c.Port = p } }
func WithTimeout(t int) Option { return func(c *Config) { c.Timeout = t } }

func main() {
	c := &Config{}
	WithPort(80)(c)
	WithTimeout(15)(c)
	fmt.Printf("%+v\n", *c)
}

Boolean Flag Options

An option without parameters can toggle a flag.

package main

import "fmt"

type Config struct{ TLS bool }
type Option func(*Config)

func WithTLS() Option { return func(c *Config) { c.TLS = true } }

func main() {
	c := &Config{}
	WithTLS()(c)
	fmt.Println(c.TLS)
}

Applying a Slice of Options

Collect options in a slice and apply each in a loop — the heart of the constructor you will build next.

package main

import "fmt"

type Config struct{ Port, Timeout int }
type Option func(*Config)

func WithPort(p int) Option    { return func(c *Config) { c.Port = p } }
func WithTimeout(t int) Option { return func(c *Config) { c.Timeout = t } }

func main() {
	c := &Config{}
	opts := []Option{WithPort(8080), WithTimeout(30)}
	for _, opt := range opts {
		opt(c)
	}
	fmt.Printf("%+v\n", *c)
}

Order Matters

Options apply in order. If two options set the same field, the last one wins — sometimes useful, sometimes a footgun to document.

Naming Convention

By convention option constructors are named With... (e.g. WithPort, WithTLS) so call sites read like English.

Options Can Hold Logic

An option is just a function, so it can validate, compute, or combine values — not merely assign.

Why Functions Beat Structs Here

  • Only set fields you mention
  • Self-documenting names at the call site
  • Add new options without breaking callers

Quick Check

Test your option function knowledge.

Recap

You learned to build option functions.

  • Define type Option func(*Config)
  • WithX constructors return closures that mutate the config
  • Apply a slice of options in a loop
  • Order matters; last write wins

Frequently asked questions

Is the “Option Functions” lesson free?

Yes — the full text of “Option Functions” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Option Functions”?

Configure with functions. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Option Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Problem with Many Params
  2. Option Functions
  3. Building a Flexible API
  4. Defaults and Validation
← Back to Go Academy