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
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:
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 ~/project
Decompress the helloWorld.tar.gz
file in the current directory:
tar -xzf helloWorld.tar.gz
After the file is extracted, check if the helloWorld
folder has been created:
ls helloWorld
This will list the contents of the helloWorld
directory if it has been decompressed correctly.
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.
GOROOT
is the directory where Go is installed, including its tools and standard library packages.GOPATH
is where your personal Go code resides, and it contains three directories:
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.
Go Modules, introduced in Go 1.11 and enabled by default in Go 1.13, aim to solve two major issues:
GOPATH/src
directory. With Go Modules, you can store your code anywhere on your system.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 testHello
Change to the testHello
directory:
cd testHello
Initialize the Go Module for this project:
go mod init testHello
The output should be:
go: creating new go.mod: module testHello
Create a new Go file testHello.go
inside the testHello
folder with the following content:
touch testHello.go
package 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.
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 helloWorld
and test
directories:
helloWorld
├── go.mod
└── helloWorld.go
test
├── go.mod
└── test.go
Next, open the test/go.mod
file. 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
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 remoteModule
Initialize the Go Module:
go mod init remoteModule
Create a file remote.go
inside the remoteModule
folder with the following content:
touch remote.go
package main
import (
"github.com/labex-labs/golang-dev-code/chap02module"
)
func main() {
chap02module.StringTobase64("miiy")
}
This program imports the chap02module
package from GitHub and uses its StringTobase64
function.
Run the remote.go
file using the following commands:
go get github.com/labex-labs/golang-dev-code/chap02module
go run -v remote.go
Tips: 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.
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 remoteModule2
Initialize the Go Module:
go mod init remoteModule2
Create a file remote2.go
with the following content:
touch remote2.go
package 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.go
The output should be:
hello
In this lab, we learned the following:
GOPATH
and GOROOT
directories.By the end of this lab, you should be comfortable using Go Modules to manage dependencies in your Go projects.