Skip to content

Language Go

Oliver Atkinson edited this page May 31, 2026 · 1 revision

Go

The Go generator emits a stand-alone .go file.

Generate Go

swift run lexicon-generate commerce.lexicon \
	--type go \
	--go-package commerce \
	-o internal/commerce/lexicon

This writes:

internal/commerce/lexicon.go

--go-package controls the package declaration. The default is lexicon.

Generated Shape

Generated Go defines:

  • I, the shared interface with ID() and Localized().
  • Lemma, a string-backed exact-path helper.
  • L, the base implementation.
  • one struct per concrete lemma.
  • constructors for generated structs.
  • exported TitleCase fields and methods for accessors.

Example:

package checkout

import "example.com/app/internal/commerce"

func TrackSubmit() string {
	submit := commerce.Commerce.Api.Order.Submit
	return submit.ID()
}

The ID() method returns the exact lemma path.

Exact-Path Helper

Generated Go includes:

func l(path string) Lemma {
	return Lemma(path)
}

Use it when a path must stay in string form, such as migration tables or test fixtures:

expected := l("commerce.api.order.submit")
if expected.ID() != commerce.Commerce.Api.Order.Submit.ID() {
	panic("unexpected vocabulary path")
}

The helper keeps exact lowercase lemma identifiers even though Go selectors are exported TitleCase.

Reserved Words and Selector Names

Go selectors are generated as exported TitleCase identifiers. A lemma path such as:

test.type.even.bad

is used from Go as:

lexicon.Test.Type.Even.Bad.ID()

The underlying ID remains:

test.type.even.bad

go generate

Add a generation directive near the package that owns the generated file:

//go:generate swift run lexicon-generate ../../lexicons/commerce.lexicon --type go --go-package commerce -o lexicon

Then run:

go generate ./...

If the Go build should not require Swift, commit the generated .go file.

Editor Support

lexicon-lsp recognizes exact-path calls in Go:

l("commerce.api.order.submit")

With editor integration, completions and unknown-path diagnostics work inside the string. The typed generated selectors are handled by normal Go tooling.

See Editor Support and GoLand and JetBrains.

Clone this wiki locally