Table of contents
1.
Introduction
2.
Keywords
3.
Example  
4.
List of keywords
5.
Identifiers
5.1.
Example
6.
Rules for Defining Identifiers
6.1.
Valid Identifiers Name
6.2.
Invalid Identifiers Name
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Identifiers & Keywords

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Golang, the identifier can be a variable, function, constant, statement labels, package deal, or type. Keywords are precise words that assist compilers in recognizing and parsing user code. In Golang, a few predeclared identifiers are available for constants, types, and functions. Those names aren't reserved and you're allowed to use them within the declaration.

Keywords

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

Example  

// Here package keyword is used to 
// include the main package in the program
package main  
// import keyword is used to 
// import "fmt" in your package
import "fmt"
// func is used to
// create function
func main() {
    // Here, var keyword is used 
    // to create variables
    // Pname, Lname, and Cname 
    // are the valid identifiers
    var Pname = "CodingNinjas" 
    var Lname = "Go Language" 
    var Cname = "Keywords"  
    fmt.Printf("Portal name: %s", Pname)
    fmt.Printf("\nLanguage name: %s", Lname)
    fmt.Printf("\nChapter name: %s", Cname)
}
You can also try this code with Online C Compiler
Run Code

Output:

Portal name: CodingNinjas
Language name: Go Language
Chapter name: Keywords
You can also try this code with Online C Compiler
Run Code

 

This example is a Go program to illustrate the use of keywords.

List of keywords

Go has only 25 keywords

Identifiers

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

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

Example

package main
import "fmt"
func main() 
{
 var name = "CodingNinjas" 
}
You can also try this code with Online C Compiler
Run Code

There is a total of three identifiers in the above example.

  1. main: Name of the package
  2. main: Name of the function
  3. name: Name of the variable

Rules for Defining Identifiers

There are particular rules for defining a valid Golang identifier. All rules should be followed; otherwise, we will get the compile-time error.

  1. The name of an identifier should begin with the letter or the underscore(_). The names may contain letters' a-z' or' A-Z' or digits 0-9 in addition to a character'_'.
  2. The name of an identifier should no longer start with a digit.
  3. The name of an identifier is case-sensitive.
  4. Keywords aren't allowed to use as an identifier name.
  5. There is no limit to the length of a name of an identifier. However, it is really useful to apply an optimum length of 4 – 15 letters most effective.

Valid Identifiers Name

Abc
ABC
abc
aBc
_abc
_Abc123
Abc123_
Abc_123
You can also try this code with Online C Compiler
Run Code

Invalid Identifiers Name

123abc
if
else
continue
default, etc. 
You can also try this code with Online C Compiler
Run Code

FAQs

  1. In the go language, which is allowed for naming an identifier?
    Only alphabetic characters, digits, and underscore.
     
  2. An identifier name must start with? 
    An identifier name must start with lower case (a to z) or upper case (A to Z) alphabets or with an underscore (_). 
     
  3. Can a keyword be used as a variable name or constant?
    No, A keyword can't be used as a variable name or constant.

Key Takeaways

In this article, we have extensively discussed Identifiers & Keywords topics and all the types and rules of defining identifiers. We learned that the program's building blocks are keywords and identifiers. A compiler uses them to define the type/kind and name of a specific variable or class function.

We hope that this blog enhances your knowledge regarding Identifiers & Keywords, and if you would like to learn more, check out our articles on Library. if you wish to know more about keywords and identifiers in other languages as well, you can visit Identifiers and keywords in C.Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass