Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Constant in inner/outer scope in Go (Golang)

Posted on September 27, 2023September 27, 2023 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
  • 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

    ©2023 Welcome To Golang By Example | Design: Web XP