Today I focused on organizing Go programs using packages, exporting functions and variables, and understanding different levels of variable scope. I improved my Conference Booking App by moving input validation logic into a separate package called helper.
Here's what I do:
Packages in Go
A package is a way to organize Go code into reusable modules.
In my app:
import (
"fmt"
"strings"
"booking-app/helper"
)
-
fmtandstringsare standard Go packages -
helperis a custom package I created - Packages allow me to separate concerns, like moving input validation out of
main()
My Go module is defined as:
module booking-app
go 1.26.0
This ensures the project is recognized as a Go module and allows me to import packages like helper.
Exported Functions & Variables
In Go, functions or variables are exported if their name starts with a capital letter.
Example from the helper package:
isValidName, isValidEmail, isValidTicketNumber := helper.ValidateUserInput(fName, lName, email, userTickets, RemainingTickets)
-
ValidateUserInputis exported because it starts with a capital letter - I can call it from main even though it’s in another package
The helper package looks like this:
package helper
import "strings"
func ValidateUserInput(fName string, lName string, email string, userTickets uint, remainingTickets uint) (bool, bool, bool) {
isValidName := len(fName) >= 2 && len(lName) >= 2
isValidEmail := strings.Contains(email, "@")
isValidTicketNumber := userTickets > 0 && userTickets <= remainingTickets
return isValidName, isValidEmail, isValidTicketNumber
}
Variables can also be exported:
var RemainingTickets uint = 50
-
RemainingTicketsis accessible across functions because it starts with a capital letter - Exporting variables allows sharing state safely between packages (but should be done carefully)
Levels of Scope in Go
There are three main types of variable scope:
- Local Scope – Variables declared inside a function, only accessible in that function:
var fName string
- Package Scope – Variables declared at the package level (outside any function) are accessible to all functions in the package:
var bookings = []string{}
- Global / Exported Scope – Variables or functions that start with a capital letter are accessible from other packages:
var RemainingTickets uint = 50
helper.ValidateUserInput(...)
Understanding scope helps control where variables are accessible and avoid accidental misuse.
How I Applied This Today
- Moved input validation logic to helper.ValidateUserInput() → cleaner main()
- Exported the function and variable so main can use them
- Learned how to structure Go programs into packages for better readability and maintainability
- Practiced using local, package, and global scope effectively in a real program
In summary, I learned how to organize Go programs using packages, which allows me to separate logic into reusable modules. I practiced exporting functions and variables so they can be accessed from other packages, like moving my input validation into a helper package. I also learned about the three levels of variable scope: local (inside functions), package (accessible within a package), and global/exported (accessible across packages).
Top comments (0)