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:
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.
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 |
|
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. |