RSS Amplifier

Ashraf Latif · Aug 5, 2025

Mastering Go 16: Go Packages – Reusing Your Code Like a Pro 📦

0
Sign in to vote or save

Ashraf Latif · Ashraf Latif

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 main

or

package mycoolstuff
  • main package = your app’s starting point (it runs func main())

  • Other packages = helper code you can reuse

Let’s build a simple package!

mathstuff/
    add.go
main.go
package 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 stuff

You can use them like this:

import "math"
math.Sqrt(16) // 4

If your app grows, split it into multiple packages:

/project
  main.go
  /utils
    file.go
  /models
    user.go

Then import what you need.

  • Packages help you reuse and organize code

  • Every Go file starts with package ...

  • Use import to use other packages

  • Built-in packages = powerful toolbox!

  • Custom packages = your own toolbox 🧰

No posts

Read the original on ceghap.substack.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.