Skip to content

Directives

Targo supports Go directives through JSDoc syntax.

Syntax

typescript
/** @go:<directive> [arguments] */

Supported Directives

DirectivePurpose
@go:buildFile build constraints
@go:generatego generate hooks
@go:embedEmbed files or directories
@go:noinlineDisable inlining
@go:nosplitDisable stack splitting
@go:noescapeMark function arguments as not escaping
@go:passthroughEmit 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.FS for directory trees, string for text, slice<byte> for binary.
  • The embed path is relative to the source file's directory.
  • Import embed from Go's standard library when using embed.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(...).