π¦ Go Package Management: Building Your Code City
Imagine youβre building a LEGO city. Each building has rooms, and each room has stuff inside. Go packages work the same way!
ποΈ The Big Picture: Your Code is a City
Think of your Go project as a city:
- The city = your whole project
- Buildings = packages (folders with Go files)
- Rooms in buildings = files inside packages
- Stuff in rooms = functions, types, variables
Why organize? Imagine a city where everything is just thrown in one giant pile. Finding anything would be impossible! Same with code.
π Package Organization: Arranging Your City
What It Means
Package organization is how you arrange buildings in your city. Each folder = one package.
Simple Example
myapp/ β Your city (project)
βββ main.go β Front door
βββ users/ β Users building
β βββ users.go
βββ orders/ β Orders building
β βββ orders.go
βββ internal/ β Private area
βββ secrets/
βββ secrets.go
Key Rule
One folder = One package name
All files in the same folder must start with the same package name.
// File: users/users.go
package users β Must match folder!
// File: users/helpers.go
package users β Same name!
Why This Rocks
- Easy to find code
- Easy to share code
- Easy to test code
π·οΈ Package Naming: Naming Your Buildings
The Golden Rules
Think of naming like labeling buildings so everyone knows whatβs inside.
| β GOOD | β BAD | Why? |
|---|---|---|
users |
userPackage |
Short wins |
http |
HTTP |
Lowercase only |
json |
jsonParser |
One word is best |
orders |
order_utils |
No underscores |
Simple Examples
// β
Perfect names
package users
package orders
package auth
// β Bad names (don't do this!)
package userManagement // Too long
package User // No capitals
package my_package // No underscores
Memory Trick
Ask yourself: βIf I saw this name in 1 second, would I know what it does?β
emailβ Handles emails βemailSendingAndValidationUtilitiesβ Confusing β
ποΈ Package Visibility: Public vs Private Rooms
The Magic Rule
This is the most important rule in Go:
| First Letter | Who Can See It? | Example |
|---|---|---|
| UPPERCASE | Everyone (public) | User |
| lowercase | Only same building (private) | user |
Think of It Likeβ¦
- Uppercase = Front door (anyone can walk in)
- Lowercase = Back room (only people in this building can enter)
Simple Example
package users
// Public - anyone can use this
type User struct {
Name string
}
// Public - anyone can call this
func CreateUser(name string) User {
return User{Name: name}
}
// private - only users package can use
func validateName(name string) bool {
return len(name) > 0
}
π€ Export Rules: What Leaves the Building
The Export Rules in 30 Seconds
graph TD A["Your Code"] --> B{First Letter?} B -->|Uppercase| C["β EXPORTED<br>Others can use"] B -->|lowercase| D["β NOT exported<br>Private to package"]
Everything That Can Be Exported
package shop
// β
All EXPORTED (uppercase first letter)
type Product struct{} // Type
var MaxItems = 100 // Variable
const TaxRate = 0.08 // Constant
func CalculatePrice() {} // Function
// β All PRIVATE (lowercase first letter)
type helper struct{} // Type
var maxRetries = 3 // Variable
const secretKey = "abc" // Constant
func validate() {} // Function
Inside Structs Too!
type User struct {
Name string // β
Public field
email string // β Private field
}
Other packages can see user.Name but NOT user.email.
π Internal Packages: The VIP Section
Whatβs an Internal Package?
Remember our city? Internal packages are like a VIP section that only certain buildings can enter.
myapp/
βββ cmd/
β βββ server/
β βββ main.go β Can use internal!
βββ internal/ β THE VIP GATE
β βββ secrets/
β βββ secrets.go
βββ pkg/
βββ public.go β Can use internal!
The Rule
Only packages in the same βfamilyβ can use internal packages.
Example
// File: internal/secrets/secrets.go
package secrets
var ApiKey = "super-secret-123"
// File: cmd/server/main.go
// β
This works - we're in the same project
import "myapp/internal/secrets"
func main() {
key := secrets.ApiKey
}
// File: some-other-project/main.go
// β ERROR! Can't import internal from outside
import "myapp/internal/secrets" // BLOCKED!
Why Use Internal?
- Hide messy code
- Keep implementation details private
- Allow changes without breaking other projects
π Package Documentation: Labels on Your Buildings
Doc Comments = Name Tags
In Go, documentation is just comments right above what youβre describing.
Simple Rules
// Package users handles user accounts.
// It provides functions to create, update,
// and delete users.
package users
// User represents a person in the system.
// Name is required; Email is optional.
type User struct {
Name string
Email string
}
// CreateUser makes a new user with the given name.
// It returns an error if name is empty.
func CreateUser(name string) (*User, error) {
// ...
}
Documentation Pattern
graph TD A["Package Comment"] --> B["// Package name does..."] C["Type Comment"] --> D["// TypeName is..."] E["Function Comment"] --> F["// FuncName does..."]
Three Golden Rules
- Start with the name being documented
- Use complete sentences
- Put comment directly above (no blank line!)
// β
GOOD - starts with function name
// CreateUser makes a new user.
func CreateUser() {}
// β BAD - doesn't start with name
// This function creates a user.
func CreateUser() {}
Run go doc to See Your Docs
go doc myapp/users # See package docs
go doc myapp/users.User # See type docs
π― Quick Summary
| Concept | One-Line Rule |
|---|---|
| Organization | One folder = One package |
| Naming | Short, lowercase, no underscores |
| Visibility | Uppercase = public, lowercase = private |
| Export Rules | First letter decides everything |
| Internal | Special folder blocks outside access |
| Documentation | Comment above, start with the name |
π Youβve Got This!
Remember the city:
- Organize your buildings (folders)
- Name them clearly
- Use CAPS for public doors
- Put secrets in internal
- Add labels (docs) so people know whatβs inside
Go package management is just common sense organization. If it feels confusing, ask: βWould this make sense in a city?β
Happy coding! ποΈ
