Welcome To Golang By Example

Menu
  • Home
  • Blog
  • Contact Us
  • Support this website
Menu

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

Posted on September 26, 2020September 26, 2020 by admin

Table of Contents

  • Overview
  • Example

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.

  • go
  • golang
  • Follow @golangbyexample

    Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2025 Welcome To Golang By Example | Design: Newspaperly WordPress Theme