AppDividend
Latest Code Tutorials

Keywords and Identifiers In Golang Explained

0

In Golang, an identifier can be a variable name, function name, constant, statement labels, package name, or types. Keywords are unique words that help compilers understand and parse user code. In Golang, there are some predeclared identifiers available for constants, types, and functions. These names are not reserved; you are allowed to use them in the declaration.

Keywords and Identifiers In Golang

In programming languages, the identifiers are used for identification purposes. In other words, identifiers are the user-defined name of the program blocks.

Identifiers in Golang

An identifier is a token that must be composed of the Unicode letters, Unicode digits and _ (underscore), and start with either a Unicode letter or _. Here,

  • Unicode letters mean the characters defined in the Letter categories LuLlLtLm, or Lo of The Unicode Standard 8.0.
  • Unicode digits mean the characters defined in a Number category Nd of The Unicode Standard 8.0.

Identifiers support the Unicode character set.

The first position of an identifier must be a letter or an underscore.

Idiomatic Go favors mixed caps (camel case) naming.

Package-level identifiers must be unique across the given package.

Identifiers must be unique within a code block (functions, control statements).

See the following code.

package main

func main() {

	var name = "AppDividend"

}

There is a total of three identifiers available in the above code.

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

Rules for Defining Identifiers

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

  1. The name of an identifier must begin with the letter or the underscore(_). And the names may contain the letters ‘a-z’ or ’A-Z’ or digits 0-9 as well as a character ‘_’.
  2. The name of an identifier should not start with a digit.
  3. The name of an identifier is case sensitive.
  4. Keywords are not allowed to use as an identifier name.
  5. There is no limit on the length of a name of an identifier, but it is advisable to use an optimum length of 4 – 15 letters only.

In Golang, there are some predeclared identifiers available for constants, types, and functions. These names are not reserved; you are allowed to use them in your declaration.

See the following list of predeclared identifiers.

For Functions:
make, len, cap, new, append, copy, close, 
delete, complex, real, imag, panic, recover

For Constants:
true, false, iota, nil

For Types:
int, int8, int16, int32, int64, uint,
uint8, uint16, uint32, uint64, uintptr,
float32, float64, complex128, complex64,
bool, byte, rune, string, error

The identifier represented by an underscore character(_) is known as the blank identifier. It is used as the anonymous placeholder instead of the regular identifier, and it has a unique meaning in declarations, as an operand, and in assignments.

The identifier which is allowed to access it from another package is known as an exported identifier.

The exported identifiers are those identifiers which obey the following conditions:

  1. The first character of the exported identifier’s name should be in the Unicode upper case letter.
  2. The identifier should be declared in the package block, or it is a variable name, or it is the method name.
  3. The uniqueness of the identifiers means an identifier is unique from the other set of identifiers available in your program or the package, and they are not exported.

Keywords in Golang

Keywords are unique words that help compilers understand and parse user code.

Until now, Go has only 25 keywords, which are the following.

break     default      func    interface  select
case      defer        go      map        struct
chan      else         goto    package    switch
const     fallthrough  if      range      type
continue  for          import  return     var

They can be categorized into four groups:

  1. The const, func, import, package, type, and var are used to declare all kinds of code items in Go programs.
  2. The chan, interface, map, and struct are used as parts in some composite type denotations.
  3. The break, case, continue, default, else, fallthrough, for, goto, if, range, return, select and switch are used to control the flow of code.
  4. The defer and go are also control flow keywords but in other particular manners.

Finally, Keywords and Identifiers In Golang article is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.