0
0
Goprogramming~10 mins

Package scope rules in Go - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Package scope rules
Start: Package declaration
Declare variables, constants, functions
Check if identifier starts with uppercase letter
Exported: Visible outside package
Package scope rules apply
Use identifiers within package or from other packages
End
This flow shows how Go decides if a variable, constant, or function is visible outside its package based on its name's first letter.
Execution Sample
Go
package main

var PublicVar = 10
var privateVar = 20

func main() {
    println(PublicVar, privateVar)
}
This code declares two variables with different visibility and prints both inside the same package.
Execution Table
StepIdentifierNameStarts with uppercase?ScopeAccessible in main()?Output
1DeclarationPublicVarYesExported (package-wide and outside)Yes
2DeclarationprivateVarNoUnexported (package-only)Yes
3main() executionPublicVarYesExportedYes10
4main() executionprivateVarNoUnexportedYes20
5Outside package accessPublicVarYesExportedYes
6Outside package accessprivateVarNoUnexportedNoError: undefined
💡 Execution stops after demonstrating visibility rules inside and outside the package.
Variable Tracker
VariableStartAfter DeclarationFinal
PublicVarundefined1010
privateVarundefined2020
Key Moments - 3 Insights
Why can main() access both PublicVar and privateVar even though privateVar starts with a lowercase letter?
Because both variables are declared in the same package, package scope allows access to unexported (lowercase) identifiers within the package, as shown in execution_table rows 3 and 4.
Why can't code outside the package access privateVar?
Because privateVar starts with a lowercase letter, it is unexported and only visible inside its own package. Execution_table row 6 shows that outside access causes an error.
What determines if an identifier is exported or unexported in Go?
The first letter of the identifier: uppercase means exported (visible outside the package), lowercase means unexported (package-only), as shown in execution_table steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at step 4, what is the value of privateVar inside main()?
A20
Bundefined
CError
D10
💡 Hint
Check the 'Output' column at step 4 in the execution_table.
At which step does the code show that an identifier is not accessible outside the package?
AStep 3
BStep 6
CStep 1
DStep 4
💡 Hint
Look for 'Error: undefined' in the 'Output' column in the execution_table.
If we rename privateVar to PrivateVar, how would step 6 change?
AIt would still cause an error
BIt would be accessible outside the package
CIt would be accessible only inside main()
DIt would be undefined inside main()
💡 Hint
Refer to the 'Starts with uppercase?' and 'Scope' columns in the execution_table steps 1 and 6.
Concept Snapshot
Go package scope rules:
- Identifiers starting with uppercase are exported (visible outside package).
- Identifiers starting with lowercase are unexported (package-only).
- Inside the same package, all identifiers are accessible.
- Outside the package, only exported identifiers are accessible.
- This controls visibility and encapsulation in Go.
Full Transcript
In Go, the visibility of variables, constants, and functions depends on their names. If the name starts with an uppercase letter, it is exported and can be accessed from other packages. If it starts with a lowercase letter, it is unexported and only accessible within the same package. For example, PublicVar is accessible everywhere, while privateVar is only accessible inside the package. The main function can access both because it is in the same package. Outside the package, privateVar is not visible and causes an error. This rule helps organize code and control what is shared between packages.