Go – Constants

As the name CONSTANTS suggests means fixed, in programming languages also it is same i.e., once the value of constant is defined it cannot be modified further. There can be any basic data types of constant like an integer constant, a floating constant, a character constant, or a string literal. 

How to declare?
Constant are declared like variables but in using a const keyword as a prefix to declare constant with a specific type. It cannot be declare using := syntax.


package main

import "fmt"

const PI = 3.14

func main() {
    const GFG = "GeeksforGeeks"
    fmt.Println("Hello", GFG)

    fmt.Println("Happy", PI, "Day")

    const Correct= true
    fmt.Println("Go rules?", Correct)


Hello GeeksforGeeks
Happy 3.14 Day
Go rules? true

Untyped and Typed Numeric Constants: 
Typed constants work like immutable variables can inter-operate only with the same type and untyped constants work like literals can inter-operate with similar types. Constants can be declared with or without a type in Go. Following is the example which show typed and untyped numeric constants that are both named and unnamed.

const untypedInteger          = 123
const untypedFloating typed   = 123.12

const typedInteger  int             = 123
const typedFloatingPoint   float64  = 123.12

Following is list of constant in Go Language:

  • Numeric Constant (Integer constant, Floating constant, Complex constant)
  • String literals
  • Boolean Constant

Numeric Constant: 
Numeric constants are high-precision values. As Go is a statically typed language that does not allow operations that mix numeric types. You can’t add a float64 to an int, or even an int32 to an int. Although, it is legal to write 1e6*time.Second or math.Exp(1) or even 1<<(‘\t’+2.0). In Go, constants, unlike variables, behave like regular numbers.

Numeric constant can be of 3 kinds i.e., integer, floating-point, complex

Integer Constant: 

  • A prefix specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
  • An integer literal can also have a suffix that is a combination of U(upper case) and L(upper case), for unsigned and long, respectively
  • It can be a decimal, octal, or hexadecimal constant.
  • An int can store at maximum a 64-bit integer, and sometimes less.

Following are some examples of Integer Constant:

85         /* decimal */
0213       /* octal */
0x4b       /* hexadecimal */
30         /* int */
30u        /* unsigned int */
30l        /* long */
30ul       /* unsigned long */
212         /* Legal */
215u        /* Legal */
0xFeeL      /* Legal */
078         /* Illegal: 8 is not an octal digit */
032UU       /* Illegal: cannot repeat a suffix */

Complex constant: 
Complex constants behave a lot like floating-point constants. It is an ordered pair or real pair of integer constant( or parameter) And the constant are separated by a comma, and the pair is enclosed in between parentheses. The first constant is the real part, and the second is the imaginary part. A complex constant, COMPLEX*8, uses 8 bytes of storage.


(0.0, 0.0) (-123.456E+30, 987.654E-29)

Floating Type Constant:

  • A floating type constant has an integer part, a decimal point, a fractional part, and an exponent part
  • Can be represent floating constant either in decimal form or exponential form.
  • While representing using decimal form, you must include the decimal point, the exponent, or both.
  • And while representing using exponential form, must include the integer part, the fractional part, or both.

Following are the examples of Floating type constant:

3.14159       /* Legal */
314159E-5L    /* Legal */
510E          /* Illegal: incomplete exponent */
210f          /* Illegal: no decimal or exponent */
.e55          /* Illegal: missing integer or fraction */

String Literals

  • Go supports two types of string literals i.e., the ” ” (double-quote style) and the ‘ ‘ (back-quote).
  • Strings can be concatenated with + and += operators.
  • A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.And this is of untyped.
  • The zero values of string types are blank strings, which can be represented with " " or '' in literal.
  • String types are all comparable by using operators like ==, != and (for comparing of same types)


type _string struct {
    elements *byte // underlying bytes
    len      int   // number of bytes

Example to show string literals:
"hello, geeksforgeeks"

“hello, \


"hello, " "geeks" "forgeeks"

Here, all of the above three statements are similar i.e., they don’t have any particular type.

package main

import "fmt"

func main() {
    const A= "GFG"
    var B = "GeeksforGeeks"
    // Concat strings.
    var helloWorld = A+ " " + B
    helloWorld += "!"
    // Compare strings.
    fmt.Println(A == "GFG")   
    fmt.Println(B < A) 


GFG GeeksforGeeks

Boolean constant: 
Boolean constants are similar to string constants. It apply same rules as string constant.The difference is only that it have two untyped constants true and false. 

package main

import "fmt"

const Pi = 3.14

func main() {
 const trueConst = true
    type myBool bool
    var defaultBool = trueConst // allowed
    var customBool myBool = trueConst // allowed
  //  defaultBool = customBool // not allowed





Leave a Reply

Your email address will not be published. Required fields are marked *