Introduction
In this lab, we will learn about two different ways of managing dependencies in Go: GOPATH and Go Modules. These are essential concepts for organizing and managing Go projects effectively.
Knowledge Points:
- GOPATH
- GOROOT
- Go Modules
Preparation
In this step, we'll prepare the environment by decompressing a provided tarball.
First, open your terminal and navigate to the project directory by typing the following:
cd ~/projectDecompress the
helloWorld.tar.gzfile in the current directory:tar -xzf helloWorld.tar.gzAfter the file is extracted, check if the
helloWorldfolder has been created:ls helloWorld
This will list the contents of the helloWorld directory if it has been decompressed correctly.
Basic Module GOPATH
Before Go 1.11, Go used a specific workspace called GOPATH to store all Go code. In this step, we'll understand how the GOPATH works.
GOROOTis the directory where Go is installed, including its tools and standard library packages.GOPATHis where your personal Go code resides, and it contains three directories:- src: Stores your project source code.
- bin: Stores compiled executable files.
- pkg: Stores compiled packages that are not executable.
To check the current GOPATH and GOROOT, run the following commands:
## Check the GOPATH directory
go env | grep GOPATH
## Check the GOROOT directory
go env | grep GOROOT
These commands will display the paths for GOPATH and GOROOT on your machine.
Initialize mod
Go Modules, introduced in Go 1.11 and enabled by default in Go 1.13, aim to solve two major issues:
- Over-reliance on GOPATH: Before Go Modules, code had to be stored inside the
GOPATH/srcdirectory. With Go Modules, you can store your code anywhere on your system. - Version dependency issues: Go Modules allow projects to manage and track dependencies with specific versions, making it easier to work with multiple versions of the same package.
In this step, we will initialize a Go Module for a new project.
Create a new directory for the project called
testHello:cd ~/project mkdir testHelloChange to the
testHellodirectory:cd testHelloInitialize the Go Module for this project:
go mod init testHelloThe output should be:
go: creating new go.mod: module testHelloCreate a new Go file
testHello.goinside thetestHellofolder with the following content:touch testHello.gopackage main import "fmt" func main() { fmt.Println("hello, world") }
Now, we have initialized the testHello project using Go Modules and added a simple Go program to it.
Importing other packages with mod
In this step, we will demonstrate how to import and use other modules in your Go project.
The helloWorld package in the helloWorld folder has been initialized with Go Modules. Similarly, the test package, which also uses Go Modules, imports the Say function from the helloWorld package.
To explore this, run the following command:
First, look at the structure of the
helloWorldandtestdirectories:helloWorld ├── go.mod └── helloWorld.go test ├── go.mod └── test.goNext, open the
test/go.modfile. You should see the following:module test go 1.15 require "helloWorld" v0.0.1 replace "helloWorld" => "../helloWorld"
The replace directive tells Go to use the local path ../helloWorld instead of fetching it from an external repository. The require directive specifies that the test package depends on version v0.0.1 of the helloWorld package.
| Parameter | Description |
|---|---|
| module | Specifies the package |
| require | Specifies dependencies |
| replace | Replaces dependencies |
| exclude | Excludes dependencies |
To run the test.go file in the test package:
cd ~/project/test
go run test.go
The output should be:
hello, world
Importing Remote Packages
In Go, you can easily import remote packages. Let's demonstrate this by using a remote package from GitHub.
Create a directory
remoteModule:cd ~/project mkdir remoteModule cd remoteModuleInitialize the Go Module:
go mod init remoteModuleCreate a file
remote.goinside theremoteModulefolder with the following content:touch remote.gopackage main import ( "github.com/labex-labs/golang-dev-code/chap02module" ) func main() { chap02module.StringTobase64("miiy") }This program imports the
chap02modulepackage from GitHub and uses itsStringTobase64function.Run the
remote.gofile using the following commands:go get github.com/labex-labs/golang-dev-code/chap02module go run -v remote.goTips: Free user can't access the internet, so you can't run this command. Upgrade to the Pro plan to get access to the internet.
The go get command will download the chap02module package from GitHub and place it in your module cache. Once that's done, the go run command will execute your program.
Mini Test
In this step, we will test our knowledge by creating a new file that uses the Hello function from the remote module.
Create a folder
remoteModule2:cd ~/project mkdir remoteModule2 cd remoteModule2Initialize the Go Module:
go mod init remoteModule2Create a file
remote2.gowith the following content:touch remote2.gopackage main import ( "github.com/labex-labs/golang-dev-code/chap02module" ) func main() { chap02module.Hello() }Run the program:
go get github.com/labex-labs/golang-dev-code/chap02module go run remote2.goThe output should be:
hello
Summary
In this lab, we learned the following:
- The meaning and purpose of
GOPATHandGOROOTdirectories. - The basics of Go Modules for managing dependencies.
- How to initialize and use Go Modules in a project.
- How to import and use local and remote Go packages.
By the end of this lab, you should be comfortable using Go Modules to manage dependencies in your Go projects.



