0
0
Goprogramming~10 mins

Creating custom packages in Go - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating custom packages
Create folder for package
Write Go files with package name
Export functions/variables (capitalized)
Import package in main program
Use exported functions/variables
Run or build program
This flow shows how to create a Go package by making a folder, writing code with a package name, exporting items, importing it elsewhere, and using it.
Execution Sample
Go
package mathutil

func Add(a, b int) int {
    return a + b
}
Defines a custom package 'mathutil' with an exported function Add that sums two integers.
Execution Table
StepActionCode LineEffectOutput/State
1Create folder 'mathutil'N/AFolder created for packageFolder 'mathutil' exists
2Write package declarationpackage mathutilDefines package namePackage named 'mathutil'
3Write exported function Addfunc Add(a, b int) int { return a + b }Function Add available outside packageAdd function ready
4In main.go, import packageimport "mathutil"Imports custom packagePackage 'mathutil' imported
5Call Add functionresult := mathutil.Add(3, 4)Calls Add with 3 and 4result = 7
6Print resultfmt.Println(result)Outputs sum7 printed
7Program endsN/AExecution completeProgram exits
💡 Program ends after printing the sum 7
Variable Tracker
VariableStartAfter Step 5Final
resultundefined77
Key Moments - 3 Insights
Why must the function name start with a capital letter to be used outside the package?
In Go, only names starting with a capital letter are exported and visible outside their package, as shown in step 3 and 5 where Add is accessible.
What happens if the package is not imported correctly in main.go?
If the import is missing or wrong (step 4), the program cannot find the package and will fail to compile or run.
Why do we create a separate folder for the package?
Go organizes packages by folders; creating a folder (step 1) helps Go find and manage the package code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'result' after step 5?
Aundefined
B3
C7
D0
💡 Hint
Check the 'Effect' and 'Output/State' columns in row for step 5.
At which step is the package 'mathutil' imported into the main program?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for the 'Action' column mentioning import in the execution table.
If the function Add was named 'add' (lowercase), what would happen when calling it in main.go?
AThe program would fail to compile because 'add' is not exported.
BIt would work normally.
CThe program would print 0.
DThe program would run but return wrong results.
💡 Hint
Refer to the key moment about capitalized names being exported.
Concept Snapshot
Creating custom packages in Go:
- Create a folder with package files
- Use 'package name' at top of files
- Export functions by capitalizing names
- Import package in main with import
- Call exported functions with package.Func
- Run program to use your package
Full Transcript
This visual execution shows how to create a custom package in Go. First, you create a folder named for your package. Inside, write Go files starting with 'package yourpackagename'. Export functions by naming them with a capital letter, like Add. In your main program, import the package using import statement with the folder path. Then call the exported functions with package name prefix. The execution table traces creating the folder, writing code, importing, calling Add(3,4), and printing the result 7. Variables like 'result' hold the sum after calling Add. Key moments explain why capitalization matters for export, the need to import packages, and folder structure. The quiz tests understanding of variable values, import steps, and export rules. The snapshot summarizes the steps to create and use custom packages in Go.