Go Constants

Go Constants

In this tutorial, we are going to discuss about constants in Golang. The term constant in Golang is used to denote fixed values such as

100  
"Waytoeasylearn" 
85.15

and so on.

Go Constants
Declaring a constant

The keyword const is used to declare a constant. Go supports constants of character, string, boolean, and numeric values. Let’s see how to declare a constant using an example.

package main

import (
     "fmt"
)

func main() {
     const a = 100
     fmt.Println(a)
}

Output

100

Run in playground

In the above code a is a constant and it is assigned the value 100.

Declaring a group of constants

There is also another syntax to define a group of constants using a single statement.

package main

import (
    "fmt"
)

func main() {
     const (
         name = "Ashok Kumar"
         age = 29
         country = "India"
     )
     fmt.Println(name)
     fmt.Println(age)
     fmt.Println(country)
}

Output

Ashok Kumar
29
India 

In the above program, we have declared 3 constants name, age and country.

Constant, as the name indicate, cannot be reassigned again to any other value.

In the program below, we are trying to assign another value 89 to a. This is not allowed since a is a constant. This program will fail to run with compilation error cannot assign to a.

package main

func main() {  
     const a = 50 
     a = 100 //not allowed
}

Run in playground

The value of a constant should be known at compile time. Hence it cannot be assigned to a value returned by a function call since the function call takes place at run time.

package main

import (
     "math"
)

func main() {
     var a = math.Sqrt(5)   //allowed
     const b = math.Sqrt(5) //not allowed
}

Run in playground

In the above example, a is a variable and hence it can be assigned to the result of the function math.Sqrt(5)

b is a constant and the value of b needs to be known at compile time. The function math.Sqrt(4) will be evaluated only during run time and hence const b = math.Sqrt(5) fails to compile with error

./prog.go:9:8: const initializer math.Sqrt(5) is not a constant
String Constants

As we discussed in previous tutorial, 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.

As we already know, 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). Following are the some examples of string literals or constants.

"hello, welcome to waytoeasylearn"
 "hello, welcome to \
 waytoeasylearn" 
"hello, " "welcome to" "waytoeasylearn"
Typed and Untyped constants

In Golang, constants are typed when you explicitly specify the type (data type) in the declaration:

package main

import (
     "fmt"
)

func main() {
     const age int = 29
     const name string = "Ashok Kumar"
     fmt.Println("Age is ", age)
     fmt.Println("Name is", name)
}

Run in playground

In Golang, constants are untyped when you don’t explicitly specify the type (data type) in the declaration:

package main

import (
     "fmt"
)

func main() {
     const age = 29
     const name = "Ashok Kumar"
     fmt.Println("Age is", age)
     fmt.Println("Name is", name)
}

Run in playground

Boolean Constants

These 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"

func main() {
     const trueConst = true
     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

Go Constants


Scroll to top