Constant in GoLang

What is Constant?

Constants are the fixed values in the program; they can't be changed during the execution process. These fixed values are also known as literals in the program.

You can also say that constant is a variable that stores the data that can't change the entire program. The only difference between the normal variable and the constant variable is its value. The constant value can't be changed, while the normal variable's value can change the program. Constant can be any data. It may be an integer or string etc.

Types of constants

Based on the data, constants can be categorized into three sections; which are given below:

  1. Numeric constant,
  2. String constant,
  3. Boolean constant.

Numeric constant:

The go language is a static type language, and it has high precision values, which means it will not permit some operations like the addition of float64 with int is not possible, and int32 with int is not possible.

A numeric constant can also be categorized in the following parts:

  1. Integer constant
  2. Float constant
  3. Complex constant

Integer constant: Here few points of the integer constants are given below. These are very important in creating an integer constant in the go program. These points are:

  1. An integer constant includes a decimal, octal, and hexadecimal constants in it.
  2. A prefix declares the base in the integer constants; it can be as follows:

>>> 0X or 0x defines the hexadecimal constants and 0 (zero) for octal constants. There is no prefix for the decimal constants.

  • An integer constant also has some suffix value, which is a combination of U and L. Both U and L can be in upper or lower case. Here U stands for unsigned suffix, and L stands for long.
  • An integer constant that has a maximum capacity to store is 64 bit.  It can store less than 64 bits but can't exceed the limit.

Example:

Here are some valid and invalid constants are given below:

  1. 95                     /* decimal constant */
  2. 024                    /* octal constant */
  3. 0x4a                /* hexa decimal constant */
  4. 0X4a                /* hexa decimal constant */
  5. 40                      /* int constant */
  6. 40u                   /* unsigned int constant */
  7. 40l                     /* long constant */
  8. 214                    /* valid constant */
  9. 216u                 /* valid constant */
  10. 0xFeeL             /* valid constant */
  11. 078                   /* Invalid constant, because 8 is not an octal number*/
  12. 012UU            /* Invalid constant, because suffix can’t be repeat. */

Complex constants:

Complex constants are mostly same as the floating type constants. It has a real pair of integer constants, or you can say that it has an ordered pair of integer constants. These constants will be distinguished with the help of a comma. The ordered or real pair will be enclosed with the parentheses. These pairs also have two parts, same as complex numbers, the first constant value will be considered as a real part, and the second constant value will be considered as an imaginary part of the complex constant.

Complex constant value stores 8-byte memory in the go program.

Example:

            (1.02, 0.12)

            (-12.334, 92.678)

            (-123.56E+40, 567.23E-39)

Float constants:

Float constants include four parts, which are as follows:

  1. Integer part
  2. Decimal part
  3. Fractional part
  4. Exponent part

The floating-point constants can be declared in two ways, which is given below:

  1. In the form of decimal: When the developer uses the decimal form to declare the floating-point constant values, it becomes necessary for him to include the decimal point (decimal part) or the exponent part. He can also use both parts together in the declaration. 
  2. In the form of exponent: When the developer uses the exponent form to declare the floating-point constant values, it becomes necessary for him to include the integer part and the fractional part. He can also use both parts together in the declaration. 

Example:

3.123                          /* valid */

31259E-4L                  /* valid */

511E                           /* Invalid, because it has incomplete exponent */

215f                            /* Invalid, because it has no decimal or exponents */

.e234                          /* Invalid, it has no integer of fraction part. */

.1234                          /* Invalid, it also has no integer or fraction part */

String Constant:

A string is a group of characters enclosed within the double quotes in the go program. A string variable can be concatenated with another string with the help of the '+' operator and '+=' operator. You can also compare strings using a comparison operator, i.e., '==' and '!='.

There are two types of the representation of string constant available in go language, which is given below:

  1. Double quote style: The string will be enclosed within the double-quotes. For example, "string." 
  2. Single quote style: The string will be enclosed with single quotes. For example: 'string.' 

String constants include the characters in it, which are very similar to character literals. These literals are:

  1. Plain characters
  2. Universal character
  3. Escape sequence

These literals have no type in go language.

You can also define string constant as having zero value in the go language. It is also known as a blank string. The blank string is represented by a pair of single quotes (' ') or in double quotes(" ") contains single whitespace.

Boolean Constant:

The Boolean constants follow all rules of the string constants because it is mostly same as string constants. There is only one difference between string and Boolean constant, i.e., the Boolean constant has one more untyped string: 'true' and false.'

Typed and untyped numeric constants

In the go program, type constants perform the same as immutable variables, while untyped constants are the same as literals. These constants can operate with the same type of variable only.

The go language allows the developer to declare the constants without using the type and untype in the program. It is optional in the declaration process.

Rules for creating a constant variable

The rules for creating a constant variable are given below:

Rule No. 1:

If a variable is declared with the same name in the go program, you can't create a constant with that name. It will result in an error.

Rule no. 2:

A keyword can't be a constant, which means the constant name can't be given on the keywords' name. For example; if, default func and other remaining keywords can't be declared a constant variable in the program. For example:

            const if = 22   //invalid syntax, because it is a keyword

Rule no.3:

If a constant is declared in the program, you can't change the constant value in another statement of the program, which means the constant value will not differ in the entire program.

Rule no.4:

A constant name can't start with a digit.

Rule no. 5:

The first letter of the constant must be an alphabet or an underscore.

Declaration format of a constant

The constants can be declared with the help of keyword in the go language. There is a special keyword available for declaration of the program's constant, which is const. This 'const' keyword will be used as a prefix with a variable name in the declaration process. You can declare constants in three patterns in the go program. These patterns are:

  1. Single line – single constant declaration
  2. Single line – multiple constants declaration
  3. Declaration in a block.

Single line – single constant declaration:

It is a normal declaration form in the go language.

For example:

                         const country  = "India"
                         const code = 91 

Single line – multiple constants declaration:

You can also define more than one constant variable in a single line of the program. Here you will use the const keyword a single time and can declare more than one constant.

Example:

                        const country, code = "India", 91

Block declaration:

It is another syntax to declare the constants in the go program. You can also define a group of constants inside a block. This block will enclose with the parenthesis and having a const keyword in prefix.

For example:

             const (
                         country = "India"
                         code = 91
                         states = 28
                         UT = 9
  ) 

Program for constant declaration:

 package main
 // Program to declare a constant variable.
 import "fmt"
 const pi = 3.14               //single line - single constant declaration
 func main() {
             const name, age = "Govind", 25           //single line - multiple constant declaration
             const a = "constants_declaration"
             const (
                         country = "India"
                         code = 91                      //Block declaration
                         states = 29
                         UT = 9
   )
             fmt.Println(a)
             fmt.Println(name, age)
             fmt.Println(pi)
             fmt.Println(country, code, states, UT)
 } 

Output:

Here, the output of the above program is given below:

Constant in GoLang