liveBook · Manning

Value parser

As explained in Go by Example

A value parser is a function that converts a string flag value to a specified Go type, assigns the result to a variable, and reports parsing errors.

A value parser converts the string value supplied for a command-line flag, assigns the converted result to an associated Go variable, and returns an error when conversion fails. It separates generic flag extraction from type-specific handling: after `-n=100` is split into a name and string value, an integer parser can set an `int`, while a string parser can set a `string`. A shared `func(string) error` signature lets a flag parser keep parsers of different target types in one map. section 4.2.1

A practical custom design uses higher-order functions. `intVar` and `stringVar` accept pointers to destination variables and return closures. Each closure retains its pointer: `stringVar` assigns directly, while `intVar` calls `strconv.Atoi`, assigns the converted integer, and returns any conversion error. Keeping the target pointer outside the common parser signature makes parsers uniform and independently testable. section 4.2.2

Bind parsers to CLI flags

Bind flag names to closures created for particular configuration fields, then look up and call the matching parser after splitting each argument. For example, `"url"` can use `stringVar(&c.url)`, while `"n"`, `"c"`, and `"rps"` use `intVar` for their integer fields. Report an unknown flag separately; wrap a parser failure with the flag name and invalid value so callers can still inspect the underlying error. section 4.2.3

Conversion is not validation. A built-in integer parser can accept any value that converts to an integer, including values that are meaningless for a program such as negative request counts or zero concurrency. Enforce program rules after basic conversion, or provide a custom parser when validation belongs with the flag value itself. section 4.4

The standard `flag` package represents a flag’s parser through `flag.Value`. A `Value` implementation provides `String() string`, used to obtain the displayed default, and `Set(string) error`, used to parse and update the linked variable. Built-in implementations include string, integer, and boolean value parsers; this narrow interface also permits custom parser types. section 4.4.1

Register a validating parser with Var

Use `FlagSet.Var` to attach a custom `flag.Value` implementation when built-in `IntVar` behavior is insufficient. Initialize the linked variable before calling `Var` to establish its default, because `Var` obtains the displayed default through `String`. During parsing, the flag set calls `Set`; a validation failure becomes an actionable command-line error. A custom positive-integer parser can therefore reject `-n 0` while accepting readable numeric input such as `10_000`. section 4.4.3

Figure 4.4 Calling intVar returns a closure that retains the variable’s pointer. Calling the closure updates the original variable’s value through the pointer.
A white background with black text

AI-generated content may be incorrect.

The closure diagram makes the key binding visible: a returned parser retains a variable pointer and updates the original variable when called. section 4.2.2

How Go by Example covers it

How deep it goes
Builds closure-based string and integer parsers, then explains `flag.Value` methods and `Var` registration for custom validation.
Examples
Uses URL and numeric CLI flags, a configuration struct, invalid zero or negative inputs, and underscore-separated request counts.
What it assumes
Assumes familiarity with Go functions and pointers while introducing closures, higher-order functions, and the `flag.Value` interface.
What you can do after
Apply it via Go's `flag` package to register custom validating flag values with `FlagSet.Var`.
How it connects
Connects closures, higher-order functions, pointers, error wrapping, `flag.Value`, `FlagSet`, and command-line flags.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go by Example. The section links point to the parts of the book it draws on.