How to Use Go Modules in a REST App
Explore how to set up and use Go modules in a RESTful web app to manage project dependencies efficiently. Understand reorganizing code structure, initializing modules, and integrating third-party libraries such as Viper to handle configurations outside the source code.
We'll cover the following...
Earlier, in the “Environment Setup and Scaffolding” lesson, we created the coffee.go file for our coffee shop.
At that time, executing the go run or go build commands via the “Run” button threw the following error:
go: go.mod file not found in current directory or any parent directory; see ‘go help modules
To counter this now, we need to initialize a module for our project.
Reorganizing the code
But before that, let's create another folder in the project root called coffee and move the file coffee.go to it.
This is done to move the files in the package to its separate folder so that the code remains organized and easy to navigate.
The project structure should now look like this:
coffeeshop|__ coffee|__ coffee.go
Initializing a module
To initialize the coffeeshop module for our web app, we'll use the following command:
Executing it returns:
For now, let's ignore line 2. We'll get to it soon enough. In line 1, we create the module coffeeshop. When we check the project ...