Identifiers


December 27, 2021, Learn eTutorial
1656

In programming languages, identifiers are the user-defined names or entities of the components in the program. An identifier is a sequence of one or more letters and digits that are used to identify the components. In Go language, an identifier can be a constant, function name, variable name, package name, types, or statement labels.


package main
import "fmt"

func main() {

 var site = "Learn eTutorials"
  
}

In the above example, there are three identifiers available:

  •     main: Name of the package
  •     main: Name of the function
  •     site: Name of the variable

Rules For Naming Identifiers

There are some rules that a programmer must follow to construct a valid Go identifier. If the programmer does not follow these rules, then compile-time errors will occur. These errors will interrupt the execution of the program. 

  1. The name of an identifier can be a combination of alphabets, underscore (_), and digits.
  2. The first letter of a Go identifier must be an alphabet or an underscore (_). You can use both lowercase and uppercase alphabets.
  3. The first letter of a Go identifier must not be a digit. The digit from 0 to 9 can only be used after the first letter.
  4. The name of the Go identifier is case-sensitive. It means that an identifier must be typed with a consistent capitalization of letters in the entire program.
  5. The keywords (or reserved words) are not allowed to be used as an identifier.
  6. In Go programming, there is no rule on how many letters an identifier can have. But it is advisable not to use more than 15 letters. 

Now let's look into the table given below to understand which are the valid identifiers and which are not.

Sample Status of Validation Rule

hello

valid

The first letter must be an alphabet.

_hello

valid

Underscore can be used as the first letter.

Hello

valid

Both lowercase and uppercase alphabets can be used.

hEllo

valid

Both lowercase and uppercase alphabets can be used.

hello123

valid

  •  The First letter must be an alphabet.
  •  Digits can be used after the first letter.

hello_world

valid

The name of an identifier can be a combination of alphabets, underscore (_), and digits.

123hello

not valid

The first letter should not be a digit.

if

not valid

The keywords are not allowed to be used as an identifier.

import

not valid

The keywords are not allowed to be used as an identifier.