Keywords in GoLang

Keywords in GoLang: Keywords are reserved words in every language. There are 25 keywords available in the go language. These keywords are pre-defined in the go language library and can't be used as an identifier in the go program. Every keyword has its specified task to perform. A keyword can't be used as a variable name or constant.

List of keywords:

The list of all 25 keywords of the Go language is given below:

breakcasechanconstcontinue
defaultdeferelsefallthroughfor
funcgogotoifimport
interfacemappackagerangereturn
selectstructswitchtypevar

Example:

A program of using keywords in Go language is given below:

 package main          // here, the package is a keyword.
 import "fmt"         // here, import is a keyword.
 func main() {        // here, func is a keyword.
             var name = "Govind"       // here, var is a keyword.
             fmt.Println(name)
 } 

Output:

The output of the above go program is given below:

Keywords in GoLang

Identifiers:

An Identifier can be the name of a variable, functions, arrays, classes, and many more like user define functions in the program. Identifiers are the basic requirements of any language, but each language has its own rules for creating these identifiers. Identifiers are used for the identification.

You can also say that an identifier is a name that is given by the developer in the program. It can be a variable name or function name or any user-defined functions in the go program."

There are no pre-defined identifiers available in the go language.

Rules for naming an identifier in the go language:

There are several rules for creating an identifier in the go language. The Go language strictly follows these rules. If the identifier does not create within the rules, the developer will get an error during the compilation process. Here are some rules are given below for naming the identifiers:

Rule no. 1:

Only alphabetic characters, digits, and underscore are allowed for naming an identifier in the go language. The developer can't use punctuation characters like @, &, #, %, *, and many more for creating the identifiers. The alphabets can be in lower case or upper case.

Rule no. 2:

The go language is a case-sensitive programming language, so the upper case and lower case variables will be distinct in the go program.

For example: if you are using two variables like var name and var Name, it will be two different variables, and both will store their distinct values in the go program.

Rule no. 3:

An identifier name must start with lower case (a to z) or upper case (A to Z) alphabets or with an underscore (_). Except for the first letter of the identifier, the remaining letters can be in any alphabets (in lower case or upper case) or underscore (_) or digits (0 to 9).

The identifier name can't start with a digit (0 to 9).

Rule no. 4:

A keyword can't be the name of an identifier in the go language. The developer can't create an identifier by the keywords' name because keywords are pre-defined in the go language library, and each keyword has its description.

Rule no. 5:

The developer can give the name of the identifier as long as he wants. There is no limit on the character's length in creating the identifier in the go program. But it is not an intelligent work to give a very long name to an identifier. The recommended length for creating an identifier name is 4 to 15 letters only.

Valid Identifiers name:

Abc

ABC

abc

aBc

_abc

_Abc123

Abc123_

Abc_123

Invalid identifiers name:

                        123abc

                        if

else

continue

default, etc.

Example:

Here is a program for printing a variable that has a string type value.

 package main                        //  here is the first identifier, i.e., main
 import "fmt"
 func main() {                                      // here is second identifier i.e. main
             var name = "Govind"           //  here is third identifier i.e. name
             fmt.Println(name)
 } 

Explanation of the program:

In the above go program, there are three identifiers used, which are:

  1. main
  2. main
  3. name

The first main is the package name, the second main is the function name, and the 3rd is the name. The 3rd identifier is a variable.

Output:

Keywords in GoLang