Scope of Variables in Go Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 13 Likes 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 Create Quiz Comment A anshul_aggarwal Follow 13 Improve A anshul_aggarwal Follow 13 Improve Article Tags : Go Language Go-Basics Golang Explore Go Tutorial 3 min read OverviewGo Programming Language (Introduction) 7 min read How to Install Go on Windows? 3 min read How to Install Golang on MacOS? 4 min read Hello World in Golang 3 min read FundamentalsIdentifiers in Go Language 3 min read Go Keywords 2 min read Data Types in Go 7 min read Go Variables 9 min read Constants- Go Language 6 min read Go Operators 9 min read Control StatementsGo Decision Making (if, if-else, Nested-if, if-else-if) 5 min read Loops in Go Language 5 min read Switch Statement in Go 2 min read Functions & MethodsFunctions in Go Language 3 min read Variadic Functions in Go 3 min read Anonymous function in Go Language 2 min read main and init function in Golang 2 min read What is Blank Identifier(underscore) in Golang? 3 min read Defer Keyword in Golang 3 min read Methods in Golang 3 min read StructureStructures in Golang 7 min read Nested Structure in Golang 3 min read Anonymous Structure and Field in Golang 3 min read ArraysArrays in Go 7 min read How to Copy an Array into Another Array in Golang? 3 min read How to pass an Array to a Function in Golang? 2 min read SlicesSlices in Golang 14 min read Slice Composite Literal in Go 3 min read How to sort a slice of ints in Golang? 2 min read How to trim a slice of bytes in Golang? 3 min read How to split a slice of bytes in Golang? 3 min read StringsStrings in Golang 7 min read How to Trim a String in Golang? 2 min read How to Split a String in Golang? 3 min read Different ways to compare Strings in Golang 2 min read PointersPointers in Golang 8 min read Passing Pointers to a Function in Go 3 min read Pointer to a Struct in Golang 3 min read Go Pointer to Pointer (Double Pointer) 4 min read Comparing Pointers in Golang 3 min read Like