Imagine writing the same code again and again — ugh! 😤
That’s where packages come in.
They’re like folders full of useful stuff — and you can use them in your program whenever you need.
A package is just a group of Go files that work together.
Every .go file must start with a line like:
package mainor
package mycoolstuffmainpackage = your app’s starting point (it runsfunc main())Other packages = helper code you can reuse
Let’s build a simple package!
mathstuff/
add.go
main.gopackage mathstuff
func Add(a, b int) int {
return a + b
}package main
import (
"fmt"
"yourmodule/mathstuff" // use your real module path
)
func main() {
result := mathstuff.Add(5, 3)
fmt.Println(result) // 8
}
Go gives you a TON of useful packages out of the box:
Package | What it does
fm | tPrinting stuff
math | Math functions like Sqrt()
os | File and environment access
strings | String functions
net/http | Web server / client stuffYou can use them like this:
import "math"
math.Sqrt(16) // 4If your app grows, split it into multiple packages:
/project
main.go
/utils
file.go
/models
user.goThen import what you need.
Packages help you reuse and organize code
Every Go file starts with
package ...Use
importto use other packagesBuilt-in packages = powerful toolbox!
Custom packages = your own toolbox 🧰
No posts

Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.