Wednesday, December 25, 2024
Google search engine
HomeLanguagesGolangConstants- Go Language

Constants- Go Language

As the name CONSTANTS suggests, it 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 type of constants like an integer constant, a floating constant, a character constant, or a string literal. 

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

Example: 

Go




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)
}


Output: 

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          = 123.12

const typedInteger  int             = 123
const typedFloatingPoint   float64  = 123.12

Following is a list of constants 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:

  1.  integer
  2. floating-point
  3. 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(lower 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. 
Example: 

(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 represented as a floating constant either in decimal form or exponential form.
  • While represented using the decimal form, it must include the decimal point, the exponent, or both.
  • And while represented using the exponential form, it must include the integer part, the fractional part, or both.

Following are examples of Floating type constants: 

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 literal 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)

Syntax:

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

Example to show string literals: 

"hello, neveropen" 

"hello, \ 

neveropen" 

"hello, " "geeks" "forgeeks" 

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

Example: 

Go




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


Output: 

GFG GeeksforGeeks!
true
false

Time Complexity: O(1)
Auxiliary Space: O(1)
 

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

Go




package main
 
import "fmt"
 
const Pi = 3.14
 
func main()
{
    const trueConst = true
     
    // Type definition using type keyword
    type myBool bool   
    var defaultBool = trueConst // allowed
    var customBool myBool = trueConst // allowed
     
    //  defaultBool = customBool // not allowed
    fmt.Println(defaultBool)
    fmt.Println(customBool)  
}


Output: 

true
true

Time Complexity: O(1)
Auxiliary Space: O(1)
 

Constant in Go: Go has constants of character, string, Boolean, and numeric values. Const declares a constant value. A const statement can occur where there is var can and thus performs arithmetic operations without any fixed precision.

Go




// Const demonstration using go.
package main
 
import (
    "fmt"
    "math"
)
 
const s string = "GeeksForGeeks"
 
func main() {
    fmt.Println(s)
 
    const n = 5
 
    const d = 3e10 / n
    fmt.Println(d)
 
    fmt.Println(int64(d))
 
    fmt.Println(math.Sin(n))
}


Output:

GeeksForGeeks
6e+09
6000000000
-0.9589242746631385

Time Complexity: O(1)
Auxiliary Space: O(1)
 

  • If you want to define multiple constants at once, you can do so using a block of brackets as below.

Example:

Go




package main
 
import "fmt"
 
const (
    GFG     = "GeeksforGeeks"
    Correct = true
    Pi      = 3.14
)
 
// Main function
func main() {
 
    fmt.Println("value of GFG : ", GFG)
 
    fmt.Println("value of Correct : ", Correct)
 
    fmt.Println("value of Pi : ", Pi)
}


Output:

value of GFG :  GeeksforGeeks
value of Correct :  true
value of Pi :  3.14

Previous article
Next article
RELATED ARTICLES

Most Popular

Recent Comments