Lesson 26Using Modules
Modules are important because they allow you to organize related code files into the same package and organize the code in a way that promotes simplicity and reusability. In this lesson, you'll learn the steps needed to set up and use modules.
GETTING STARTED WITH MODULES
From a code perspective, a module is a collection of Go packages and files along with a file called go.mod
. In the following steps, you'll learn how to create a module and then use it.
STEP 1: CREATE THE PROJECT DIRECTORY
First, create a directory with the same name that will be used for your module. In our example, you use the module and directory name mymodule
. Create this directory under $GOPATH/src
. On our system, this is /opt/homebrew/Cellar/go/1.17.6/libexec/src/mymodule
. You can create a directory on Linux by entering the following commands on the command line:
user src % mkdir mymodule
user src % cd mymodule
user mymodule % ls
user mymodule %
On a Windows system, in a command window, you can create the new mymodule
directory as follows:
C:\User\YourName\go\src> md mymodule
C:\User\YourName\go\src> cd mymodule
C:\User\YourName\go\src> dir
Directory of C:\Users\YourName\go\src\mymodule
04/20/2022 11:22 AM <DIR> .
04/20/2022 11:22 AM <DIR> ..
STEP 2: ...
Get Job Ready Go now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.