Directives
Targo supports Go directives through JSDoc syntax.
Syntax
typescript
/** @go:<directive> [arguments] */Supported Directives
| Directive | Purpose |
|---|---|
@go:build | File build constraints |
@go:generate | go generate hooks |
@go:embed | Embed files or directories |
@go:noinline | Disable inlining |
@go:nosplit | Disable stack splitting |
@go:noescape | Mark function arguments as not escaping |
@go:passthrough | Emit raw Go comments or directives |
@go:build — Build Constraints
Place at the top of a file to control when it is included in the build:
typescript
/** @go:build "linux && amd64" */
export function platformSpecific(): void {
// only compiled on linux/amd64
}@go:generate — Code Generation
Trigger go generate commands:
typescript
/** @go:generate "stringer -type=Status" */@go:embed — File Embedding
Embed files or directories into the binary. The directive must be placed on a variable declaration:
typescript
/** @go:embed "static/*" */
const staticFiles: embed.FS = zero<embed.FS>();For embedding a single file as a string:
typescript
/** @go:embed "version.txt" */
const version: string = "";For embedding a single file as bytes:
typescript
/** @go:embed "data.bin" */
const data: slice<byte> = zero<slice<byte>>();Key rules:
- The variable type determines the embed mode:
embed.FSfor directory trees,stringfor text,slice<byte>for binary. - The embed path is relative to the source file's directory.
- Import
embedfrom Go's standard library when usingembed.FS.
@go:noinline / @go:nosplit / @go:noescape
Compiler hints placed directly above a function declaration:
typescript
/** @go:noinline */
function hotPath(x: int): int {
return x * 2;
}@go:passthrough
Emit raw Go comments or directives that Targo does not natively understand:
typescript
/** @go:passthrough "//go:linkname localFunc runtime.someFunc" */Rules
- Use JSDoc comments directly above the declaration they affect.
- Keep directive strings Go-valid; Targo passes them through to generated Go.
- Do not use placeholder syntaxes like
@directive(...).