Identifiers in GO

Introduction:

Identifiers in any language are name of variable, function, array, struct etc. The same applicable for GO. There are certain rule and convention for identifier in GO.

Rules:

1. Identifier should start with letters or underscore.
2. It can contain numbers but it should not be at the start. Number can place in middle or end of identifier.
3. Identifier are case sensitive. It means variables result and Result are two different variables.
4. Go keywords can not be used as identifier. Go has 25 reserved keywords:

5. Go has some predefined identifier. These are not reserved. You can use them in your program. It sounds weird but all basic datatypes are also predefined datatypes, you can use them as variable name in-fact as all identifiers. Below are the list of predefined identifiers:

package main
import ("fmt")
//import "time"

func main() {
 var int int =10
 fmt.Println(int+2)
}
Even though it is allowed, I personally recommend that you should not use them. It will create confusion.

6. Case of first letter of any identifier is very important. If an identifier starts with small letter, it is not exported, it will be only visible to files of package it belongs. If is starts with capital letter, it is exported, it will be visible to all the packages. An identifier declared within a function will be visible to that particular function only.

Conventions

1. There is not limit of length of an identifiers but it is recommended to use short and meaningful names.
2. Go recommend to use camel case when there are multiple words are used in an identifier.

Comments

Popular posts from this blog

Data types and Literals in Java

How to define Auxiliary Constructor in case class in Scala

equals() method of Object class in Java