liveBook · Manning

Build tag

As explained in Learn Go with Pocket-Sized Projects

A build tag is a source-file constraint that controls whether the file is included for a particular build configuration.

An Arduino Nano 33 IoT needs pin names that match its board. A build tag lets TinyGo include the board’s pin-mapping file only for a matching build configuration. Go and TinyGo also use build tags to select files for operating systems and architectures. section 12.3.1

Listing 12.18 board_arduino_nano33.go: Exposed pins
//go:build arduino_nano33 #1

// This contains the pin mappings for the Arduino Nano33 IoT board.
...
const (
   D13 Pin = PA17 #2
   LED = D13 #3
 )
...

The first line of the board file is `//go:build arduino_nano33`. When that tag is selected, the file supplies pin definitions such as `LED`. section 12.3.1

Use the constraint in the file, then select the configuration at build time:

  • Put `//go:build {tag}` on the first line. A file whose required tag is not selected is ignored. section 12.3.1
  • Pass tags with `-tags`: for example, `tinygo build --tags=cortex-m0plus,atsamd21g18a,sam,arduino_nano33 main.go`. The flag also works with test and run commands. section 12.3.1
  • Express alternatives, combinations, or exclusions in the file constraint with `||`, `&&`, and `!`; `//go:build !windows` excludes Windows. section 12.4
Listing 12.23 targets/arduino-nano33.json: Target specifications
{
    "inherits": ["atsamd21g18a"], #1
    "build-tags": ["arduino_nano33"], #2
    "flash-command": "bossac -i -e -w -v -R -U --port={port} #3
        --offset=0x2000 {bin}",  #3
    "serial-port": ["acm:2341:8057", "acm:2341:0057"],  #3
    "flash-1200-bps-reset": "true"  #3
}

A TinyGo target specification can supply build tags. The Arduino Nano 33 target lists `arduino_nano33`, allowing `tinygo build --target=arduino-nano33 main.go` instead of spelling out the tags. section 12.3.1

Keep the constraint beside the implementation it controls. For a board-specific program, check that the chosen tags—or target—include the pin mappings the program uses. section 12.3.1

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines file placement and inclusion, then shows command-line selection, Boolean constraints, and target-supplied tags.
Examples
Uses an Arduino Nano 33 pin-mapping file, a TinyGo build command, and a target specification.
What it assumes
Assumes familiarity with Go source files and build commands while introducing TinyGo’s board-specific pin mappings.
What you can do after
Operate/configure it by selecting board-specific files with `-tags` or a TinyGo target.
How it connects
Connects build constraints to TinyGo targets, microcontroller pin mappings, operating systems, and architectures.

Also explained in

Compare how 3 books explain build tag →

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.