0
0
Goprogramming~5 mins

Package scope rules in Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is package scope in Go?
Package scope means that a variable, function, or type is accessible anywhere within the same package but not outside it.
Click to reveal answer
beginner
How do you make a function or variable accessible outside its package in Go?
By starting its name with an uppercase letter, making it exported and accessible from other packages.
Click to reveal answer
beginner
Given this code snippet, where is the variable accessible?
package main

var count int = 5

func main() {
    // ...
}
The variable 'count' is accessible anywhere inside the 'main' package but not from other packages.
Click to reveal answer
intermediate
What happens if two files in the same package declare variables with the same name at package scope?
It causes a compile-time error because package scope variables must have unique names within the package.
Click to reveal answer
intermediate
Explain the difference between package scope and block scope in Go.
Package scope means the identifier is visible throughout the entire package, across all files. Block scope means the identifier is only visible within the enclosing block where it is declared (e.g., inside a function or if statement).
Click to reveal answer
In Go, how do you make a variable accessible outside its package?
AStart its name with an uppercase letter
BDeclare it inside the main function
CUse the 'public' keyword
DPrefix it with 'export_'
Where can a package-scoped variable be accessed?
AOnly inside the function where it is declared
BAnywhere in the program
CAnywhere inside the same package
DOnly inside the file where it is declared
What happens if two package-scoped variables have the same name in the same package?
ANo problem, both exist separately
BCompile-time error
CThe second variable overwrites the first
DRuntime error
Which of these is NOT true about package scope in Go?
AUnexported identifiers are only accessible inside the package
BPackage scope variables are accessible across all files in the package
CExported identifiers are accessible outside the package
DPackage scope variables are accessible from other packages by default
If a variable name starts with a lowercase letter, what is its visibility in Go?
AVisible only inside its package
BVisible everywhere in the program
CVisible only inside its function
DVisible only inside its file
Describe how package scope works in Go and how it affects variable and function visibility.
Think about uppercase vs lowercase naming.
You got /3 concepts.
    Explain why two package-scoped variables cannot have the same name in Go and what error occurs if they do.
    Consider how Go enforces uniqueness in package scope.
    You got /3 concepts.