Package scope rules in Go - Time & Space Complexity
When we write Go code, variables and functions can be visible in different places. Package scope means they are visible throughout the whole package.
We want to understand how this visibility affects how often code runs and how that grows with input size.
Analyze the time complexity of the following code snippet.
package main
import "fmt"
var count int // package scope variable
func increment(n int) {
for i := 0; i < n; i++ {
count++
}
}
func main() {
increment(1000)
fmt.Println(count)
}
This code uses a package-level variable count that the increment function updates in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop inside
incrementthat runsntimes. - How many times: Exactly
ntimes, wherenis the input toincrement.
As n grows, the number of times the loop runs grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 increments of count |
| 100 | 100 increments of count |
| 1000 | 1000 increments of count |
Pattern observation: The work grows directly with the input size. Double the input, double the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the input size.
[X] Wrong: "Because count is package scoped, the loop runs multiple times automatically."
[OK] Correct: Package scope only means the variable is visible everywhere in the package. It does not cause the loop to run more times. The loop runs exactly as many times as coded.
Understanding how package scope affects code visibility helps you reason about how often code runs and where variables change. This skill is useful when explaining code behavior clearly.
What if the increment function called itself recursively instead of using a loop? How would the time complexity change?
Practice
Solution
Step 1: Understand Go naming conventions
In Go, identifiers starting with a lowercase letter are unexported, meaning they are private to the package.Step 2: Apply package scope rule
Such variables cannot be accessed from outside their package, ensuring encapsulation.Final Answer:
It is private to the package and cannot be accessed from other packages. -> Option AQuick Check:
Lowercase start = private [OK]
- Thinking lowercase variables are accessible outside package
- Confusing lowercase with constants
- Assuming lowercase means global
Solution
Step 1: Recall Go export rules
Functions starting with an uppercase letter are exported and accessible outside the package.Step 2: Check each option
func MyFunction() {} starts with uppercase 'M', so it is exported. Others start with lowercase or invalid characters.Final Answer:
func MyFunction() {} -> Option AQuick Check:
Uppercase start = exported function [OK]
- Using lowercase to export functions
- Using underscores which are allowed but don't affect export
- Confusing export with naming style
package mathops
var result = 10
func Result() int {
return result
}
What will happen if another package tries to access mathops.result?Solution
Step 1: Identify variable scope
The variable 'result' starts with lowercase, so it is unexported and private to the package.Step 2: Understand access from other packages
Accessing 'mathops.result' from outside the package is not allowed and causes a compile-time error.Final Answer:
It will cause a compile-time error due to unexported variable. -> Option BQuick Check:
Lowercase variable = inaccessible outside package [OK]
- Assuming private variables can be accessed
- Thinking runtime panic occurs instead of compile error
- Confusing function export with variable export
package example
func calculate() int {
return 42
}
func Calculate() int {
return calculate()
}
What is the main reason for defining both calculate and Calculate functions?Solution
Step 1: Analyze function names and scope
'calculate' starts lowercase, so it is private to the package; 'Calculate' starts uppercase, so it is exported.Step 2: Understand design pattern
This pattern allows internal logic in 'calculate' while exposing a public function 'Calculate' that calls it.Final Answer:
To have a private helper function and a public wrapper function. -> Option DQuick Check:
Lowercase private + uppercase public wrapper [OK]
- Thinking both functions are exported
- Expecting compile error from similar names
- Assuming no reason for two functions
GetData but keeps the data source variable private. Which code snippet correctly implements this using package scope rules?Solution
Step 1: Identify private data variable
The data source variable should start lowercase to be private to the package, so 'datasource' is correct.Step 2: Identify exported function
The function 'GetData' starts uppercase, so it is exported and accessible outside the package.Step 3: Verify function returns private variable
The function returns the private variable, exposing data safely.Final Answer:
package data var datasource = "secret" func GetData() string { return datasource } -> Option CQuick Check:
Lowercase var + uppercase func = private data, public access [OK]
- Making data variable exported by uppercase
- Making function unexported by lowercase
- Returning unexported function instead of exported
Ready to prove it?
Pass the test and this pattern moves unknown
