Skip to content

Generate Elements

An element defines one value derived for every selected struct field.

elements:
- name: "json"
input:
mode: "tag"
tag_priority: ["json"]
output:
mode: "constant"

Uses the first non-empty tag in tag_priority. If no listed tag exists, the element is omitted for that field.

input:
mode: "tag"
tag_priority: ["db", "json"]

For json:"display_name,omitempty", Constago uses display_name; tag options after the first comma are removed.

Always derives the value from the Go field name. tag_priority receives its normal default when omitted, but it is not consulted in this mode.

input:
mode: "field"
tag_priority: ["field"]

Uses the first tag match, then falls back to the field name.

input:
mode: "tagThenField"
tag_priority: ["json"]

Given:

type User struct {
DisplayName string `json:"display_name"`
}
output:
mode: "constant"
format:
prefix: "JSON"

Generates:

const JSONUserDisplayName = "display_name"

This mode generates an initialized package variable with a grouped anonymous struct:

output:
mode: "struct"
format:
prefix: "JSON"
var JSONUser = struct {
DisplayName string
}{
DisplayName: "display_name",
}

Use it as JSONUser.DisplayName.

No standalone constant or grouped variable is emitted. The value remains available to getters that list the element in returns.

The element’s format controls generated names:

format:
holder: "pascal"
struct: "pascal"
prefix: "JSON"
suffix: "Key"
initialisms: ["DB"]
defaultInitialisms: true
  • holder formats fields inside grouped accessor variables.
  • struct formats top-level constant names and grouped accessor variable names.
  • prefix and suffix wrap the struct and field portions.
  • Formats are camel, pascal, snake, and snakeUpper.

The camel and pascal formats preserve conventional Go casing for common initialisms and their plurals. For example, json_api_id becomes jsonAPIID or JSONAPIID, while user_ids becomes userIDs or UserIDs. This applies to ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, and XSS.

Use initialisms to add project-specific values such as DB. Set defaultInitialisms: false to disable the built-in list and recognize only the explicit list. Both properties inherit from the root output configuration when omitted here.

With prefix: JSON, suffix: Key, and struct: snakeUpper, the constant for User.DisplayName is JSON_USER_DISPLAY_NAME_KEY.

Transforms affect the generated string value, not its Go identifier:

transform:
tag_values: true
value_case: "upper"
value_separator: "_"
initialisms: ["DB"]
defaultInitialisms: true
  • tag_values: false leaves tag-derived values unchanged.
  • Field-name fallbacks are always transformed.
  • value_case accepts asIs, camel, pascal, upper, or lower.
  • value_separator joins detected words with the configured string.
  • camel and pascal use the same Go initialism and plural rules as identifier formats.
  • initialisms and defaultInitialisms override the root output settings for this transform.

For a DisplayName field, lower plus _ produces display_name.