liveBook · Manning

Compiler directive

As explained in Learn Go with Pocket-Sized Projects

A compiler directive is a source-level instruction that affects compilation or directs related tooling without being an ordinary executable statement.

A compiler directive is a source-level instruction written as a special comment, such as //go:{something}, that affects compilation or related code-analysis tooling rather than executing as ordinary code. section 8.5.2

Embedding a corpus

Go's embed package uses a directive to include a file in the compiled program. Place the directive and target variable at package scope, import embed with the blank identifier, and the file contents become available through englishCorpus. section 8.5.2

//go:embed corpus/english.txt
var englishCorpus string

The compact listing shows the directive syntax and the variable receiving the embedded file contents. section 8.5.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains directive syntax, package-scope placement, compiler handling, and blank imports, but does not develop directive implementation internals.
Examples
Demonstrates //go:embed corpus/english.txt, a package-level string variable, and a blank import of embed.
What it assumes
Assumes familiarity with Go packages and variables while teaching the special comment syntax and required package-level placement.
What you can do after
Apply it via Go's embed package to include a regular file's contents in compiled source.
How it connects
Connects compiler directives with Go's embed package, comments, compilation, and code-analysis tools such as linters.

Also explained in

Compare how 2 books explain compiler directive →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.