Go course

Packages and ModulesModules and the go Command

Go

Display

Theme

Packages and Modules · Reading

Modules and the go Command

A module is a collection of packages versioned together, usually one repository = one module. The module's identity is written in the go.mod file in the project's top-level folder.

Creating a module: go mod init

mkdir kedai && cd kedai
go mod init github.com/rina/kedai

This command creates go.mod:

module github.com/rina/kedai

go 1.26
  • The module line is the module path. Every package inside the project is imported with this prefix, for example github.com/rina/kedai/menu for the menu/ folder.
  • The go line states the minimum Go version required.

The module path doesn't have to be a URL that actually exists. For a personal project that won't be published, a simple name like kedai is fine too. But if you want others to be able to use it with go get, the path must match the repository's location.

Project structure with several packages

kedai/
├── go.mod
├── main.go            package main     (import "github.com/rina/kedai/menu")
├── menu/
│   ├── menu.go        package menu
│   └── harga.go       package menu     (satu folder = satu package)
└── internal/
    └── db/db.go       package db       (hanya bisa di-import dari dalam module kedai)

One folder = one package, so menu.go and harga.go share the menu package. A folder named internal has a special rule: the packages inside it can only be imported by code in the same module (more precisely, by code under the parent folder of internal). That's how Go hides packages from outside users; here, db can only be imported from inside the kedai module.

Running and building

go run .              # kompilasi + jalankan package main di folder ini
go run main.go        # menjalankan file tertentu (cocok untuk skrip kecil)

go build              # menghasilkan executable bernama sesuai module/folder
go build -o kedai-app # menentukan nama file output

go test ./...         # menjalankan semua tes di semua package
go vet ./...          # mencari kesalahan umum yang lolos kompilasi
gofmt -w .            # merapikan format kode (atau: go fmt ./...)

In short: go run . compiles and runs the main package in the current folder, and go run main.go runs one specific file (handy for small scripts). go build produces an executable named after the module or folder, and -o sets the output file name. go test ./... runs every test in every package, go vet ./... looks for common mistakes that still compile, and gofmt -w . tidies up code formatting (or use go fmt ./...).

./... means "this folder and every subfolder below it".

Third-party dependencies: go get and go mod tidy

go get github.com/google/uuid@v1.6.0   # tambah/ubah dependensi ke versi tertentu
go get github.com/google/uuid@latest   # versi terbaru
go mod tidy                            # tambahkan yang kurang, buang yang tidak dipakai

go get adds or changes a dependency at a specific version (or @latest for the newest one), and go mod tidy adds whatever is missing and removes whatever isn't used. After that, go.mod contains a require block:

module github.com/rina/kedai

go 1.26

require github.com/google/uuid v1.6.0

Go also creates go.sum, which holds a checksum for every dependency to make sure the downloaded code hasn't changed. Commit both files to Git, and don't edit them by hand.

Versions follow semantic versioning, vMAJOR.MINOR.PATCH. If a module releases major version 2 or later, its import path changes too, for example example.com/lib/v2.

Installing tools: go install

go install golang.org/x/tools/cmd/goimports@latest

go install compiles a program and puts its executable in $(go env GOPATH)/bin (or GOBIN if it's set). Make sure that folder is on your PATH so you can call the command directly. The difference from go build: go build puts the result in the current folder, while go install installs it into the global bin folder.

Summary

CommandWhat it does
go mod init <path>creates a new go.mod
go run .compiles + runs
go buildproduces an executable
go install pkg@versiinstalls an executable into the bin folder
go get pkg@versiadds/changes a dependency
go mod tidytidies up go.mod and go.sum
go test ./...runs tests

This lesson is a reading only. The platform handles go.mod for you, so the next exercises go straight to packages from the standard library, which don't need go get at all.