Golang Identifiers- Variables, Constant, Keywords

Learn via video courses
Topics Covered

Overview

Identifiers are used in programming languages for identification purposes. In other words, identifiers are the user-defined names of program components. An identifier in Go can be a variable name, constant, function name, statement label, package name, or type. A variable is the name of an allocated, reserved space in memory, while a constant is a type of variable whose value in your program will remain constant throughout during execution of the program. The keywords are pre-defined in the go language library and cannot be used as identifiers within a go program.

What are Identifiers in Golang?

An Identifier can be a name for variables, functions, arrays, classes, and other user-defined functions. Identifiers are essential components of any programming language. However, each language has its own set of rules for creating Identifiers. In simple words, an identifier is a name assigned by the developer within the program. It could be the name of a variable, a function, or any user-defined function in the go program.

Examples of Identifiers

Let's take a simple go program to understand the term identifiers more meaningfully.

In the above go program, we used three identifiers:

  1. main:- Name of the package.
  2. main:- Name of the function(entry point of go program).
  3. helloMessage:- Name of a string type variable.

Rules for Defining Identifiers in Golang?

In Go, we must follow some rules while defining the name of an identifier. If we fail to do so, then we will get a compilation error.

  • The identifier name in the Go language is case-sensitive.
  • The identifier's name in Go should not begin with a digit.
  • The name of the identifier may be as long as you like, but it's best to keep the length of these identifiers between 4 to 15 letters.
  • Keywords are not accepted as valid identifier names.
  • The identifier's name in the Go language must begin with a letter or an underscore(_). Names may also contain the letters 'a-z' or 'A-Z,' the numerals ranging from 0 to 9, and the symbol '_'.

Let's look at some valid and invalid identifiers:

Valid Identifiers

Invalid Identifiers

What are Variables in Golang?

A variable is the name of an allocated, reserved space in memory. In other words, it is the name of the memory location. It is a combination of two words, varies and able, meaning its value can be changed by the user.

For example, suppose we declare an integer variable ‘x,’ then it gets some memory in the Random Access Memory. The name of this memory is variable, and it's x in this case.

Variables in Golang

Rules of Naming Variables

Like other programming languages, Go also has rules regarding naming or declaring a variable, similar to the rules followed in the case of identifiers.

  • The variable names in the Go language are case-sensitive.
  • The variable's name in the Go language must begin with a letter or an underscore(_). Names may also contain the letters 'a-z' or 'A-Z,' the numerals ranging from 0 to 9.
  • The name of the variable in Go should not begin with a digit.
  • The variable's name may be as long as you like, but it's best to keep the length of these identifiers between 4 to 15 letters.
  • Keywords are not accepted as valid variable names.

Ways to Declare Variables

Let's look at various ways of declaring variables in Go.

1. Single Variable Declaration Without Initialization

We can declare a variable in Go without initializing a value to it. In such cases, a default value (generally, it is 0) is assigned to such variables.

Syntax

Example

Output

Explanation

In Go, if a variable is not assigned a value, the default value is used. We haven't provided any value to the val_1 and val_2 variables in the above program, so it prints 0 (the default value for int and float32).

2. Single Variable Declaration With Initialization

We can declare a variable in Go and initialize some initial value to it. We can easily change the variable's value in the entire program anytime we want.

Syntax

Example

Output

3. Multiple Variable Declarations Without Initialization

In Go, we can declare multiple variables of the same type simultaneously. In such cases, a default value (generally, it is 0) is assigned to all variables.

Syntax

Example

Output

Explamation

This program prints 0 because the default value assigned in Go in case of int is 0.

4. Multiple Variable Declarations With Initialization

We can declare multiple variables in Go and initialize some initial values for each. Also, we can easily change any variable's value in the entire program anytime we want.

Syntax

Example

Output

5. Declare Variables of Multiple Types

We can also declare multiple variables at the same time in go. A single var keyword can be used to declare many variables of different data types. Since string b in the example below has no assigned value, an empty string by default is used in its place.

Syntax

Example

Output

6. Variable Declaration with No Type

The variable's data type is not explicitly specified here. The compiler automatically finds the type by analyzing the variable's value in this situation.

Syntax

Example

Output

7. Short Variable Declaration

In Go, we can use the := operation instead of var and type to declare a variable and assign a value to it.

Syntax

Example

Output

What are Constants in Golang?

The constant is derived from the Latin word "standing firm," which means "steady." A constant value in your program will remain constant throughout during execution of the program. A variable can vary during the runtime, whereas a constant does not change; it remains constant. This allows users to declare a variable that will remain constant during the program's compilation.

How to Declare Constants in Golang?

Constants are declared similarly to variables, but with the const keyword as a prefix to declare a constant of a specified type. It is not possible to declare it using the "=" syntax.

Syntax

or

Example

Types of Constants in Golang

The various types of constants in the Go language are as follows:

  1. Typed and untyped numeric constant
  2. Numeric Constant
  3. String Literals
  4. Boolean Constant

Typed and Untyped Numeric Constant

Typed constants, like immutable variables, can only interact with other variables of the same type, whereas untyped constants, like literals, can interact with variables of identical types. In Go, constants can be specified with or without a type. The following example shows both named and unnamed typed and untyped numeric constants.

In simple words, Typed Numeric constants are defined by their data type. They can no longer hold values of any other data type once declared. Untyped Numeric constants, on the other hand, are stated without specifying their data type. Type inference is used to determine the data type. The constant val1 is untyped, whereas val2 is typed into the program below.

Output

Numeric Constant

Numeric constants are values with a high degree of precision. Golang does not permit operations that mix numeric types because it is a statically typed language. You can't add a float64 to an int or even an int32 to an int. Constants, unlike variables, operate like regular numbers in Go.

Numeric Constant can also be classified into three types:

  1. Integer
  2. Floating-point
  3. Complex

Integer Constant

Integer constants are assigned integer values such as decimal, octal, and hexadecimal.

Output

Floating-Point

Floating-point constants have integer, decimal, fractional, and exponent parts.

Output

Complex

Complex constants act quite similarly to floating-point constants. It is an ordered pair or real pair of integer constants (or parameters) separated by a comma, and the pair is enclosed in parentheses. The first constant represents the real part, while the second represents the imaginary part. COMPLEX*8 is a complex constant that requires 8 bytes of storage.

Output

String Literals

Their values are wrapped in double quotations. Constants for typed strings contain the data type string in their syntax. Type inference determines the data type of untyped constants. Constant message is a typed string constant.

Output

Boolean Constant

Boolean constants are similar to string constants. It follows the same principles as a string constant. The sole distinction is that it has two untyped constants, true and false.

Output

What are the Keywords in Golang?

Keywords, often known as reserved words, are terms in a programming language that are used for internal processes or to indicate preset activities. As a result, certain words are not permitted to be used as identifiers. A compile-time error will be generated if you do this.

There are 25 keywords that are available in the go language. Inside the go program, each keyword has a specific task to do. The keyword cannot be used as a variable name or constant.

List of Golang Keywords

The keywords in the Go language are as follows:

breakdefaultfuncinterfaceselect
casedefergomapstruct
packagegotochanelseswitch
constfallthroughrangetypecontinue
ifimportreturnforvar

List of keywords

Conclusion

  • Identifiers are the user-defined names of program components. An identifier in Go can be a variable name, constant, function name, statement label, package name, or type.
  • A variable is the name of an allocated, reserved space in memory.
  • A constant is a type of variable whose value in your program will remain constant throughout during execution of the program.
  • The keywords are pre-defined in the go language library and cannot be used as identifiers within a go program.
  • Typed constants, like immutable variables, can only interact with other variables of the same type, whereas untyped constants, like literals, can interact with variables of identical types.