Skip to content
API and data contracts

closedomain

Finds builtin-string fields used as closed semantic domains.

AvailabilityOpt-in
Fix offeredNo

What it detects

Finds exported string fields that are used like a fixed set of choices—for example, a status that is repeatedly assigned or compared with the same small group of values—but are still declared as plain strings.

Checks

Check What it detects
closed-string-domain*
Reports exported string fields used as small closed sets of values.

* Opt-in; requires explicit selection.

Why this is flagged

A plain string or integer can hold values that the program does not actually support. A named type with constants makes invalid values harder to introduce, helps tools find every use, and makes missing cases easier to spot.

How to fix it

Create a named type for the set and define a constant for each supported value. Convert strings or numbers from users and external systems at the boundary, rejecting any value that is not part of the set.

Examples

Flagged code

type Job struct {
// gohawk: field State uses a closed string domain; define a named string type and constants
State string
}
func finished(job Job) bool {
return job.State == "done" || job.State == "failed"
}

Accepted code

type TaskState string
const (
TaskDone TaskState = "done"
TaskFailed TaskState = "failed"
)
type Task struct {
State TaskState
}