Welcome To Golang By Example

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

Constant in inner/outer scope in Go (Golang)

Posted on September 27, 2020September 27, 2020 by admin

Table of Contents

  • Overview
  • Example

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

  • 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