Scope of Variables in Go Last Updated : 01 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Prerequisite: Variables in Go Programming Language The scope of a variable defines the part of the program where that variable is accessible. In Go, all identifiers are lexically scoped, meaning the scope can be determined at compile time. A variable is only accessible within the block of code where it is defined.Examplepackage main import "fmt"// Global variable declaration var myVariable int = 100 func main() { // Local variable inside the main function var localVar int = 200 fmt.Printf("Inside main - Global variable: %d\n", myVariable) fmt.Printf("Inside main - Local variable: %d\n", localVar) display() }func display() { fmt.Printf("Inside display - Global variable: %d\n", myVariable) }Syntaxvar variableName type = value Table of ContentLocal VariablesGlobal VariablesLocal Variable PreferenceLocal VariablesLocal variables are declared within a function or a block and are not accessible outside of it. They can also be declared within loops and conditionals but are limited to the block scope.Example: Go package main import "fmt" func main() { var localVar int = 200 // Local variable fmt.Printf("%d\n", localVar) // Accessible here } Output200 Global VariablesGlobal variables are defined outside any function or block, making them accessible throughout the program.Example: Go package main import "fmt" // Global variable declaration var myVariable int = 100 // Global variable func main() { fmt.Printf("%d\n", myVariable) // Accessible here } Output100 Local Variable PreferenceWhen a local variable shares the same name as a global variable, the local variable takes precedence within its scope.Example Go package main import "fmt" // Global variable declaration var myVariable int = 100 // Global variable func main() { var myVariable int = 200 // Local variable fmt.Printf("Local variable takes precedence: %d\n", myVariable) // Accesses local variable } OutputLocal variable takes precedence: 200 Comment More infoAdvertise with us Next Article Scope of Variables in Go A anshul_aggarwal Follow Improve Article Tags : Go Language Go-Basics Golang Similar Reads Golang Tutorial - Learn Go Programming Language This Golang tutorial provides you with all the insights into Go Language programming, Here we provide the basics, from how to install Golang to advanced concepts of Go programming with stable examples. So, if you are a professional and a beginner, this free Golang tutorial is the best place for your 10 min read Go Programming Language (Introduction) Go is a procedural programming language. It was developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but launched in 2009 as an open-source programming language. Programs are assembled by using packages, for efficient management of dependencies. This language also supports env 11 min read How to Install Go on Windows? Prerequisite: Introduction to Go Programming Language Before, we start with the process of Installing Golang on our System. We must have first-hand knowledge of What the Go Language is and what it actually does? Go is an open-source and statically typed programming language developed in 2007 by Robe 3 min read How to Install Golang on MacOS? Before, we start with the process of Installing Golang on our System. We must have first-hand knowledge of What the Go Language is and what it actually does? Go is an open-source and statically typed programming language developed in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but 4 min read Hello World in Golang Hello, World! is the first basic program in any programming language. Letâs write the first program in the Go Language using the following steps:First of all open Go compiler. In Go language, the program is saved with .go extension and it is a UTF-8 text file.Now, first add the package main in your 3 min read Identifiers in Go Language In programming languages, identifiers are used for identification purposes. In other words, identifiers are the user-defined names of the program components. In the Go language, an identifier can be a variable name, function name, constant, statement label, package name, or type. Example: package ma 3 min read Go Keywords Keywords or Reserved words are the words in a language that are used for some internal process or represent some predefined actions. These words are therefore not allowed to use as an identifier. Doing this will result in a compile-time error. Example: C // Go program to illustrate the // use of key 2 min read Data Types in Go Data types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows: Basic type: Numbers, strings, and booleans come under this category.Aggregate type: Array and structs come under this category.Reference type: Pointer 7 min read Go Variables A typical program uses various values that may change during its execution. For Example, a program that performs some operations on the values entered by the user. The values entered by one user may differ from those entered by another user. Hence this makes it necessary to use variables as another 9 min read Constants- Go Language As the name CONSTANTS suggests, it means fixed. In programming languages also it is same i.e., once the value of constant is defined, it cannot be modified further. There can be any basic data type of constants like an integer constant, a floating constant, a character constant, or a string literal. 6 min read Like