Go Constants: Difference between revisions
(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...") |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
* [[Go_Language#Constants|Go Language]] | * [[Go_Language#Constants|Go Language]] | ||
=Overview= | =Overview= | ||
A constant is a typed expression whose value is | A constant is a typed expression whose value is created at compile time, and cannot be changed once declared, unlike a [[Go_Variables#Overview|variable]], whose value can be changed at runtime. The expressions that define constants must be [[Go_Language#Constant_Expression|constant expressions]], evaluatable by the compiler. The compiler detects modification attempts and throws a compilation error. The type is inferred from the right-hand side of the assignment. | ||
Constants are declaring using the <code>[[Go_Language#const_keyword|const]]</code> keyword in the [[Go_Language#Package_Block|package block]] and in functions. Constants can only be numbers, characters (runes), strings or booleans. | |||
Multiple constants can be assigned at the same time using enclosing parentheses. | |||
<syntaxhighlight lang='go'> | <syntaxhighlight lang='go'> | ||
const <constant_identifier> [constant_type] = <initial_value_literal> | const <constant_identifier> [constant_type] = <initial_value_literal> | ||
Line 15: | Line 19: | ||
) | ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Naming= | =Naming= | ||
The names of constants in Go follow the | The names of constants in Go follow the [[Go_Style#Naming|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 <code>maxLength</code>, not <code>MaxLength</code> or <code>MAX_LENGTH</code>. | ||
=Enumerations= | =Enumerations= | ||
Go does not have formal enums, but the language allows for sets of related, yet distinct constants. | Go does not have formal enums, but the language allows for sets of related, yet distinct constants. | ||
{{Internal|Go Enumerations#Overview|Go Enumerations}} | {{Internal|Go Enumerations#Overview|Go Enumerations}} |
Latest revision as of 23:13, 12 September 2024
External
Internal
Overview
A constant is a typed expression whose value is created at compile time, and cannot be changed once declared, unlike a variable, whose value can be changed at runtime. The expressions that define constants must be constant expressions, evaluatable by the compiler. The compiler detects modification attempts and throws a compilation error. The type is inferred from the right-hand side of the assignment.
Constants are declaring using the const
keyword in the package block and in functions. Constants can only be numbers, characters (runes), strings or booleans.
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.