Creating custom packages in Go - Performance & Efficiency
When we create custom packages in Go, we want to know how the time it takes to run our code changes as we use these packages more.
We ask: How does adding package code affect the speed of our program?
Analyze the time complexity of the following code snippet.
package mathutils
func Sum(numbers []int) int {
total := 0
for _, num := range numbers {
total += num
}
return total
}
This package function sums all numbers in a list by looping through each one once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the input list.
- How many times: Exactly once for each number in the list.
As the list gets bigger, the time to add all numbers grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: Doubling the list size doubles the work needed.
Time Complexity: O(n)
This means the time to run grows directly with the number of items we add.
[X] Wrong: "Using a package always makes code slower because it adds extra steps."
[OK] Correct: The package code runs just like any other code, so its speed depends on what it does, not just that it is a package.
Understanding how your package code runs helps you write clear and efficient programs, a skill that shows you know how to build good software.
"What if the Sum function used recursion instead of a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of packages
Packages help group related code together for reuse and clarity.Step 2: Identify the main benefit
Custom packages allow you to reuse code in different parts of your program or in other programs.Final Answer:
To organize code into reusable parts -> Option AQuick Check:
Custom packages = reusable code [OK]
- Thinking packages speed up code execution
- Believing packages remove the need for functions
- Assuming packages eliminate imports
Solution
Step 1: Recall Go export rules
In Go, only names starting with an uppercase letter are exported (visible outside the package).Step 2: Check function names
Only 'MyFunction' starts with uppercase, so it is exported.Final Answer:
func MyFunction() {} -> Option AQuick Check:
Uppercase start = exported function [OK]
- Using lowercase to export functions
- Using underscores to export
- Assuming snake_case exports functions
mathutil with an exported function Square, what is the output of this code?package main
import (
"fmt"
"mathutil"
)
func main() {
fmt.Println(mathutil.Square(4))
}Solution
Step 1: Understand the function call
The code calls mathutil.Square(4), which should return 4 squared (4*4).Step 2: Calculate the expected output
4 multiplied by 4 equals 16, so the output is 16.Final Answer:
16 -> Option BQuick Check:
Square(4) = 16 [OK]
- Assuming function name prints as string
- Thinking import fails without path
- Expecting zero as default output
package main
import (
"fmt"
"mypkg"
)
func main() {
fmt.Println(mypkg.helper())
}Assuming
helper is a function in mypkg.Solution
Step 1: Check function visibility rules
In Go, only functions starting with uppercase letters are exported and accessible outside their package.Step 2: Identify the function name issue
The function is named 'helper' (lowercase), so it is not accessible from main package.Final Answer:
Function name should start with uppercase to be accessible -> Option DQuick Check:
Lowercase function = not exported [OK]
- Thinking package name must be Main
- Believing import path must be URL
- Assuming fmt.Println can't print function output
stringutil with a function Reverse that reverses a string. Which of the following is the correct way to define and use it in your main.go?Solution
Step 1: Define exported function in package
The function must start with uppercase 'Reverse' to be accessible outside the package.Step 2: Import package and call function with package name
In main.go, import "stringutil" and call the function as stringutil.Reverse("hello").Final Answer:
Define func Reverse and call stringutil.Reverse("hello") after import -> Option CQuick Check:
Uppercase function + import + package prefix = correct usage [OK]
- Using lowercase function name and expecting export
- Calling function without package prefix after import
- Not importing the custom package
Ready to prove it?
Pass the test and this pattern moves unknown
