📖 2 min read (~ 300 words).

Build tags

Go files can be guarded by //go:build constraints. By default codescan loads a package under the default build configuration, so tag-gated files are skipped. Options.BuildTags passes the tags through to the package loader, so the annotations in those files are scanned too.

The package has an always-present model plus this one, in a file that opens with the constraint //go:build experimental:

// Experimental is only scanned when the "experimental" build tag is set.
//
// swagger:model
type Experimental struct {
	// Beta flags a beta-only feature.
	Beta bool `json:"beta"`
}

Full source: docs/examples/shaping/buildtags/experimental.go

Scanned with no tags and with experimental, the gated Experimental model appears only in the second:

Default
{
  "Stable": {
    "type": "object",
    "title": "Stable is always scanned.",
    "properties": {
      "name": {
        "description": "Name is the feature name.",
        "type": "string",
        "x-go-name": "Name"
      }
    },
    "x-go-package": "github.com/go-openapi/codescan/docs/examples/shaping/buildtags"
  }
}

Full source: docs/examples/shaping/buildtags/testdata/off.json

BuildTags: experimental
{
  "Experimental": {
    "type": "object",
    "title": "Experimental is only scanned when the \"experimental\" build tag is set.",
    "properties": {
      "beta": {
        "description": "Beta flags a beta-only feature.",
        "type": "boolean",
        "x-go-name": "Beta"
      }
    },
    "x-go-package": "github.com/go-openapi/codescan/docs/examples/shaping/buildtags"
  },
  "Stable": {
    "type": "object",
    "title": "Stable is always scanned.",
    "properties": {
      "name": {
        "description": "Name is the feature name.",
        "type": "string",
        "x-go-name": "Name"
      }
    },
    "x-go-package": "github.com/go-openapi/codescan/docs/examples/shaping/buildtags"
  }
}

Full source: docs/examples/shaping/buildtags/testdata/on.json

codescan.Run(&codescan.Options{
    Packages:   []string{"./..."},
    ScanModels: true,
    BuildTags:  "experimental",
})

BuildTags accepts the same comma-separated form as go build -tags.