Welcome To Golang By Example

Constant in inner/outer scope in Go (Golang)

Table of Contents

Overview

A constant declared within an inner scope having a same name as constant declared in the outer scope will shadow the constant in outer scope.

Example

package main
import "fmt"
const a = 123
func main() {
    const a = 456
    fmt.Println(a)
}

Output

456

The above program prints 456 as output because the inner constant declared has that value