Go Constants

  • date 10th August, 2022 |
  • by Prwatech |
  • 0 Comments

A Variable in which a value cannot be changed.

The ‘const’ keyword declares the variable as “constant”. That means it is unchangeable and read-only.

Syntax:

Const CONSTNAME type  =  value

Package main
Import “fmt”

Const PI=3.14

Func main() {
Fmt.Println(PI)
}
  1. Typed constants

Simple variable deceleration Go Lang program.

package main

import "fmt"

const X int = 10

func main() {
    fmt.Println(X)
}

Output :

PS C:\GO_Language\constant> go run const1.go
Value of X : 10

 

2.Untyped constants

Program to illustrate untyped variables.

package main

import "fmt"

const X = 11 //untyped constants/without a type

func main() {
    fmt.Println(X)
}

Output :

PS C:\GO_Language\constant> go run const2.go
Value of X : 11

 

 

3.Program to illustrate mixed Constants.

package main

import "fmt"

const (
    X int = 1
    Y     = 3343.4
    Z     = "Prwatech"
)

func main() {
    fmt.Println(X)
    fmt.Println(Y)
    fmt.Println(Z)

}

Output :

PS C:\GO_Language\constant> go run const3.go
Integer Value : 1
Float Value   : 3343.4
String Value  : Prwatech

 

 

 

Quick Support

image image