Go Constants

From NovaOrdis Knowledge Base
Revision as of 00:59, 4 July 2024 by Ovidiu (talk | contribs) (Created page with "=External= * https://go.dev/ref/spec#Constants =Internal= * Go Language =Overview= A constant is a typed expression whose value is known at compile time, and cannot be changed once declared. The compiler detects modification attempts and throws a compilation error. The type is inferred from the right-hand side of the assignment. Multiple constants can be assigned at the same time using enclosing parentheses. <syntaxhighlight lang='go'> const <co...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

External

Internal

Overview

A constant is a typed expression whose value is known at compile time, and cannot be changed once declared. The compiler detects modification attempts and throws a compilation error. The type is inferred from the right-hand side of the assignment. Multiple constants can be assigned at the same time using enclosing parentheses.

const <constant_identifier> [constant_type] = <initial_value_literal>
const a = 1
const (
	b = 1.1
	c = "something"
)

Naming

The names of constants in Go follow the general naming conventions for identifiers. While other languages name their constants using all upper caps, Go does not do that. In Go, an unexported constant is "maxLength", not "MaxLength" or "MAX_LENGTH".

Enumerations

Go does not have formal enums, but the language allows for sets of related, yet distinct constants.

Go Enumerations