Open In App

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: 




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: 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: 

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: 

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 

Syntax:

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

Example to show string literals: 

"hello, geeksforgeeks" 

"hello, \ 

geeksforgeeks" 

"hello, " "geeks" "forgeeks" 

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

Example: 




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. 




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.




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

Example:




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

Article Tags :