Go Local Modules and Packages - Understand How to Create and Use Them
Introduction
When building applications in Go, modularizing your code into separate modules and packages is essential for maintaining clean and reusable code. In this tutorial, we’ll walk through creating two local Go modules: restapi01/backend
and restapi/maincaller
. The backend
module will serve as a basic HTTP server that outputs a simple HTML view, while the maincaller
module will call functions from backend
.
We shall look into:
- Initializing Go modules and packages.
- Using
go mod edit
and go mod tidy
to set up local imports without publishing packages. - Testing the interaction between these modules and calling one from the other.
Let’s get started!