Welcome To Golang By Example

Can constant be reassigned after its declaration in Go (Golang)

Table of Contents

Overview

Constant Variable cannot be reassigned after its declaration as it is constant and its value cannot change. If you try to reassign to a constant variable then it will raise a compilation error.

Example

For example below code will raise a compilation error

package main
func main() {
    const a int = 8
    a = 9
}

Output

main.go:5:4: cannot assign to a

In the above program we created a constant first

const a int = 8

Then we try to assign a new value 9 to constant a, hence it raises a compilation error as constant once declared cannot be reassigned.