The Go target-language layer for Skmtc generators (@skmtc/lang-go). It covers the projection base factories, GoSnippet, the register shape with package imports, the four identifier kinds, one folder as one package, the head and value render model, methods as members of a type, pointers for absent values, and the producers for structs, functions, blocks and names. Use ALONGSIDE skmtc-generator whenever a generator writes Go.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Is that skill safe to install?
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
Are you the author of Skmtc Lang Go?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/skmtc-skmtc-lang-go)
---
name: skmtc-lang-go
version: 0.1.0
description: >
The Go target-language layer for Skmtc generators (@skmtc/lang-go). It
covers the projection base factories, GoSnippet, the register shape
with package imports, the four identifier kinds, one folder as one
package, the head and value render model, methods as members of a type,
pointers for absent values, and the producers for structs, functions,
blocks and names. Use ALONGSIDE skmtc-generator whenever a generator
writes Go.
metadata:
internal: true
---
# The Go layer (@skmtc/lang-go)
Read `skmtc-generator` first. The two worked examples are
`@skmtc/gen-go-json` (a model generator) and `@skmtc/gen-go-http` (an
SDK: an Index for each service, and an accumulator for the client).
Generated code targets Go 1.24 or later. The style of both follows
`google/go-github`.
## 1. Declare the language
The import graph declares the language. `src/base.ts` imports a factory,
and each snippet extends `GoSnippet`.
```ts fragment
import { GoName, toGoModelProjectionBase } from '@skmtc/lang-go'
export const GoJsonBase = toGoModelProjectionBase<EnrichmentSchema>({
id: denoJson.name,
toIdentifierName({ ref }) {
return new GoName(toModelName(ref)).toExported()
},
toIdentifierType,
toExportPath({ ref, enrichments, variant }) {
const name = this.toIdentifierName({ ref, enrichments, variant })
return `@/${new GoName(name).toFileName()}.go`
},
})
```
The factories are `toGoModelProjectionBase`,
`toGoOasOperationProjectionBase` and `toGoOasOperationIndexBase`.
## 2. A folder is a package
Every file of one folder is one Go package. A file reads a name of its
own package with no import.
- **Put every generated file in one folder.** The engine registers an
import for a definition in another file. `GoFile` renders nothing for
it when the file is in the same folder, and stops the run when it is
not: an import of another package needs the path of the Go module,
which the engine does not know.
- **Each name is in the whole package.** Two files cannot each declare
`Response`. A model generator gives a model another name when a
hand-written file of the package has it (`reservedNames` in
`gen-go-json/src/base.ts`).
- **The `package` line comes from the folder.** `'@/'` is the project's
`basePath`. A file at the top is in the folder that `basePath` names:
`./pkg/pet-store` gives `package petstore`. With no folder name that Go
can use, the package is `api`.
- **A file name can be a build rule.** `go build` reads `pets_test.go` as
a test and `pets_windows.go` as a file for Windows only. It cuts the
name at the first dot, so `pets_windows.generated.go` is for Windows
only too. It leaves out `_pets.go`, a folder that starts with `_` or
`.`, and a folder named `testdata` or `vendor`. `GoFile` stops the run
for each of these export paths. It does not read the folders of
`settings.basePath`: `go build` leaves a folder out only below the
folder where it runs, and the engine does not know where that is.
`GoName.toFileName()` gives a name with no such rule:
`MaintenanceWindows` is `maintenance_windows_file`.
- **Two files of the package can declare one name, and the engine does
not see it.** A file knows its own definitions only. An inline type
named `BasketItem` beside a component `BasketItem` compiles to
`BasketItem redeclared in this block`. `go build` reports it.
## 3. Register
| Caller | Call | File |
|---|---|---|
| Projection | `this.register(args)` | Its own export file |
| Projection | `this.registerInto(path, args)` | The file you name |
| Snippet | `this.register({ ...args, destinationPath })` | The file you name |
`args` is `{ imports?, definitions?, custom? }`. There is no `reExports`.
- Go imports a package, never a name: `imports: ['context', 'net/http']`.
An alias is `{ path: 'net/url', alias: 'neturl' }`.
- **Go refuses a file that imports a package and does not read it.** So
the producer that writes `url.PathEscape` registers `'net/url'`, in its
constructor. A producer that is not built registers nothing.
- The file renders its imports in two sorted groups: the standard
library, then every other package.
- With no `custom`, a file starts with
`// Code generated by Skmtc. DO NOT EDIT.`. Go tools read that line as
the mark of generated code.
## 4. Identifier kinds
| Kind | Factory | Renders |
|---|---|---|
| `type` | `createType(name)` | `type Pet ` + value |
| `alias` | `createAlias(name)` | `type Tags = ` + value |
| `func` | `createFunction(name)` | `func newServices` + value |
| `method` | `createMethod(name, { receiver })` | `func (s *PetsService) Get` + value |
- A `type` is a type of its own, with none of the methods of the type it
is made from. An `alias` is a second name of one type and keeps its
methods. `time.Time` and `json.RawMessage` write their own JSON, so a
model that is one of them must be an alias.
- The receiver of a `method` is the `typeName` of its identifier. In
`toIdentifierType`, return
`{ type: 'method', typeName: '*PetsService' }`.
- Go has no keyword for visibility. A name that starts with a capital is
exported, so `exported` and `noExport` render nothing.
## 5. Head and value
The identifier renders the head of a declaration, and the value renders
all that follows. The value of a struct `type` is a `GoStruct`, and the
value of a `func` or a `method` is a `GoFunctionValue`. A projection
holds one and renders it.
**A comment is above its declaration**, where the value cannot write
it. A definition reads the `description` field of its value:
```ts fragment
export class GoJsonProjection extends GoJsonBase {
value: TypeSystemValue
description: string | undefined
…
}
```
## 6. A method is a member of a type
Go declares a method outside its type, at the top level of the file. An
Index is the type, and its members are the methods:
```ts fragment
this.value = new GoMethodSet({ underlying: 'service', methods: this.definitions })
```
`GoMethodSet` renders the type and then each method as a declaration of
its own. Give it the `definitions` array of the Index itself: the
methods arrive after the constructor, and the method set reads the array
when it renders. A member projection states `toIndex: () => TheIndex`
and no `toExportPath`.
## 7. Pointers
The zero value of a Go type (`0`, `""`, `false`) is a value of its own.
A value that can be absent or `null` is a pointer, and `nil` is the
absent value. `GoPointer({ type, pointer })` writes the star.
- A slice, a map, `any` and `json.RawMessage` can be `nil`. They are
never pointers.
- `time.Time` is not a pointer: its zero value is the absent value, and
the `omitzero` tag leaves it out.
- A struct cannot hold itself. A reference inside a cycle
(`modelRef.suspended || modelRef.inCycle`) is a pointer.
- Decide the pointer in one class (`GoJsonOptional` in `gen-go-json`).
A field that can be absent has the tag option `omitzero`, which leaves
out a `nil` pointer, a `nil` slice or map and a zero `time.Time`.
## 8. Indentation
`gofmt` writes a tab for each level. A producer renders its own lines
from column 0. `GoBlock` and `GoIndented` are the only code that writes
a tab, so blocks nest to any depth. Do not write a tab in a template.
A producer does not align the columns of a struct. A project sets
`settings.formatter` to `"gofmt -w"`.
## 9. Producers
| Producer | Renders |
|---|---|
| `GoList.toArguments(items)`, `.toLines`, `.toParagraphs` | `a, b`, one item on each line, a blank line between items |
| `GoBlock(statements, { separator? })` | `{`, the statements one level deeper, `}`. `{}` for none |
| `GoIf({ condition, statements })` | `if err != nil { … }` |
| `GoStruct(fields)` | `struct { … }` |
| `GoField({ name, type, tags?, description? })` | `Name string` and its tag, with its comment above |
| `GoTag({ key, name, options? })` | `json:"name,omitzero"`. The name `-` with no option is written `-,`. A name with a comma, a quote, a backslash or a backtick stops the run |
| `GoOmitTag({ key })` | `url:"-"`: the package leaves the field out |
| `isJsonTagName(name)` | Whether `encoding/json` reads the name from a tag: not empty, and letters, digits, spaces and common signs only. For any other name it uses the Go name of the field, with no error. Call it on each property name in your router case for an object, and throw there with the pointer of the schema. `GoTag` does not make this check |
| `GoFunctionValue({ parameters, results, statements })` | `(ctx context.Context) (*Pet, error) { … }` |
| `GoParameter({ name, type })` | `petID int64` |
| `GoPointer({ type, pointer })` | `*string`, or `string` |
| `GoConstBlock({ description?, constants })`, `GoConstant` | `const ( … )` |
| `GoMethodSet({ underlying, methods })` | A type, then its methods |
| `GoPathFormat(path)` | `"pets/%v"`, and `.placeholders` |
| `GoComment(text)` | `// text` on each line, or nothing |
An item that is `undefined` or `null` is absent. `GoBlock`, `GoStruct`
and `GoMethodSet` read their array when they render.
## 10. Names and literals
- `new GoName(text).toExported()` for a type, a field, a method and a
constant: `petId` gives `PetID`. `.toUnexported()` for a parameter and
a variable: `petID`. `.toFileName()` for a file: `pet_id`. Each form
is a valid Go name for any text, and writes Go's initialisms (`ID`,
`URL`, `API`) in one case. A text with no letter and no digit gives
`X`, `x` and `x`.
- `toUnexported()` gives a keyword and a name that Go declares itself a
trailing underscore (`type_`, `string_`). A parameter named `string`
would hide the type.
- `new GoNameScope(reserved).claim(name)` makes the names of one list
unique: the fields of a struct, the parameters of a function. Two keys
can give one Go name (`user-id` and `user_id`). Put the names that a
function declares itself in `reserved`, with the packages that it
reads: a parameter named `url` hides the package of `url.PathEscape`.
- `toStringLiteral(value)` writes a string of the document as a Go
string. Use it for each key, path and enum value. It escapes a byte
order mark, which Go refuses in a file. `GoComment` leaves one out.
## 11. A declaration and a type position differ
An object with properties is a struct where it is declared, and only a
name where a type is written. The router contract has no argument for
the position. `gen-go-json` solves this as follows:
- `toGoJsonDeclaration(typeName)` returns the router for the declaration
of a type. `toGoJsonType` is the router for a type position.
- In a type position, an inline object declares a struct beside the
definition that uses it and renders its name. The name comes from the
schema's `stackTrail`: `toInlineTypeName` of this package gives it.
- Look for that struct with `findInlineType`, not with
`context.findDefinition`, which compares the name and the file only:
```ts fragment
findInlineType(context, { name, stackTrail: objectSchema.stackTrail, destinationPath }) ??
this.defineAndRegister({ identifier: createType(name), value: struct, destinationPath })
```
A second walk of one schema gets the struct of the first. A name that
is the struct of another schema fails the run. Make the value of the
struct with `stackTrail: objectSchema.stackTrail.clone()`.
- `toIdentifierType` gives the kind of the definition: `type` or
`alias`. The lint rule `skmtc/single-dispatch` allows a test of
`schema.type` there.
- A generator that writes types of its own (the parameters of a method)
calls the exported `toGoJsonType`.
## 12. Pitfalls
| Symptom | Fix |
|---|---|
| `"fmt" imported and not used` | An import was registered by a producer that does not write the name. Register it in the producer that does |
| `undefined: url` | The producer that writes `url.PathEscape` did not register `'net/url'` |
| `Pet redeclared in this block` | Two files of the package declare one name. Give one of them another name in `base.ts` |
| `invalid recursive type` | A struct holds itself by value. Make a reference inside a cycle a pointer |
| A `time.Time` field is written as `{}` | The model is a `type` made from `time.Time`. Declare it as an `alias` |
| `refers to a definition in '…', which is in another folder` | Put the two files in one folder in `toExportPath` |
| `cannot be a Go file` | `go build` would leave the file out or build it for one target. Make the name with `GoName.toFileName()` in `toExportPath` |
| `cannot be in a 'json' struct tag` | A property name has a comma, a quote, a backslash or a backtick, and no tag can hold it. The field needs marshal code of its own: clone the generator and write it |
| `The method '…' has no receiver` | Return `typeName` with `type: 'method'` from `toIdentifierType` |
| A type renders with no methods | `GoMethodSet` got a copy of `definitions`. Give it the array itself |
| A zero (`0`, `""`) is not sent | The field is a value with `omitzero`. A value that can be absent is a pointer |
| `has two schemas for the type 'OrderShipTo'` | Two inline schemas give one type name (section 11). Move one to `components.schemas` under its own name, or change the name in a clone of the generator |