Go¶
Overview¶
The Go Generator produces idiomatic Go code from a LinkML model: structs for
classes, const blocks for enums, JSON tags for serialization, and struct
embedding for inheritance. Custom Jinja2 templates can be supplied via
--template-dir to override any of the built-in templates.
Docs¶
Command Line¶
gen-golang¶
Generate Golang structs from a LinkML schema.
This generator produces idiomatic Go code with: - Structs for classes - Const blocks for enums - JSON tags for serialization - Struct embedding for inheritance
gen-golang [OPTIONS] YAMLFILE
Options
- -V, --version¶
Show the version and exit.
- --template-dir <template_dir>¶
Optional jinja2 template directory to use for Go code generation.
Pass a directory containing templates with the same name as any of the default GolangTemplateModel templates to override them. The given directory will be searched for matching templates, and use the default templates as a fallback if an override is not found.
Available templates to override:
- module.go.jinja- struct.go.jinja- field.go.jinja- enum.go.jinja- imports.go.jinja
- --named-slot-types, --no-named-slot-types¶
Generate named Go types for parent slots with is_a children for type safety.
- Default:
False
- --nullable-primitives, --no-nullable-primitives¶
Use pointer types for optional primitives so omitempty only triggers on nil.
- Default:
True
- --alphabetical-sort, --no-alphabetical-sort¶
Sort structs and enums alphabetically for deterministic output.
- Default:
False
- -C, --config-file <config_file>¶
Path to a gen-project-style YAML config file setting ‘generator_args: {golang: {package: …}}’. An explicit –package always takes precedence over the config file.
- --package-name <package_name>¶
DEPRECATED alias for –package
- --package <package>¶
Override the Go package name (default: derived from schema name)
- -f, --format <format>¶
Output format
- Default:
'go'- Options:
go | golang
- --metadata, --no-metadata¶
Include metadata in output
- Default:
True
- --useuris, --metauris¶
Use class and slot URIs over model uris
- Default:
True
- -im, --importmap <importmap>¶
Import mapping file
- --log_level <log_level>¶
Logging level
- Default:
'WARNING'- Options:
CRITICAL | ERROR | WARNING | INFO | DEBUG
- -v, --verbose¶
Verbosity. Takes precedence over –log_level.
- --mergeimports, --no-mergeimports¶
Merge imports into source file (default=mergeimports)
- --stacktrace, --no-stacktrace¶
Print a stack trace when an error occurs
- Default:
False
Arguments
- YAMLFILE¶
Required argument
Code¶
- class linkml.generators.golanggen.GolangGenerator(schema: str | ~typing.TextIO | ~linkml_runtime.linkml_model.meta.SchemaDefinition | Generator | ~pathlib.Path, schemaview: ~linkml_runtime.utils.schemaview.SchemaView = None, format: str | None = None, metadata: bool = True, useuris: bool | None = None, log_level: int | None = 30, mergeimports: bool | None = True, source_file_date: str | None = None, source_file_size: int | None = None, logger: ~logging.Logger | None = None, verbose: bool | None = None, output: str | None = None, namespaces: ~linkml_runtime.utils.namespaces.Namespaces | None = None, directory_output: bool = False, base_dir: str = None, metamodel_name_map: dict[str, str] = None, importmap: str | ~collections.abc.Mapping[str, str] | None = None, emit_prefixes: set[str] = <factory>, metamodel: ~linkml.utils.schemaloader.SchemaLoader = None, stacktrace: bool = False, include: str | ~pathlib.Path | ~linkml_runtime.linkml_model.meta.SchemaDefinition | None = None, template_file: str = None, true_enums: bool = False, use_aliases: bool = False, package: str | None = None, gen_slots: bool = True, sort_imports: bool = True, add_json_tags: bool = True, use_time_package: bool = True, alphabetical_sort: bool = False, nullable_primitives: bool = True, named_slot_types: bool = False, template_dir: str | ~pathlib.Path | None = None)[source]¶
Generates Golang code from a LinkML schema.
This generator creates idiomatic Go structs with JSON tags from LinkML class definitions. It supports: - Struct generation from classes - Const blocks from enums - Inheritance via struct embedding - Proper type mappings - Multivalued fields as slices - Required vs optional fields
Package Configuration¶
The generated Go package clause is driven by the following precedence:
--packagecommand-line option, orpackage=...when usingGolangGeneratorprogrammatically (--package-name/package_name=...is a deprecated alias)generator_args.golang.packageset via--config-file/-C(see below)Fallback: derived from the schema name – lowercased, truncated at the first underscore, with any character outside
[a-z0-9_]stripped (e.g. a schema namedkitchen_sinkyieldspackage kitchen)
The derived fallback is always a legal Go package name: a name that collides with a
Go reserved word is suffixed with an underscore (similar to pythongen.py, type_test
produces package type_), and one that strips down to nothing – for example, a
schema named _private, – falls back to example. A --package or config-file
value, by contrast, is never rewritten: an invalid one is reported as an error rather
than silently corrected.
Configuration File¶
As an alternative to --package, gen-golang accepts a --config-file/-C
YAML file – the same format used by gen-project’s own --config-file
(see Project Generator) and by gen-java (see Java), so a single
project-wide config.yaml can be shared between them. package lives under
generator_args.golang:
# config.yaml
generator_args:
golang:
package: mypackage
gen-golang only ever reads generator_args.golang.package out of this file –
every other key is ignored, so a full multi-generator project config.yaml can be
passed as-is without modification.
Deprecation note¶
The --package-name option still works but is deprecated in favour of
--package, which is the canonical option name across package-scoped generators
(matching gen-java). Using --package-name emits a deprecation warning.
The same rename applies to the generator’s constructor: GolangGenerator takes
package, the same field JavaGenerator takes, so the two are configured
identically in code. package_name=... remains accepted as a deprecated alias, and
reading .package_name off an instance still returns the package; both emit the
same deprecation warning.