GOPATH and Module

GolangGolangBeginner
Practice Now

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.

  1. First, open your terminal and navigate to the project directory by typing the following:

    cd ~/project
  2. Decompress the helloWorld.tar.gz file in the current directory:

    tar -xzf helloWorld.tar.gz
  3. 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.

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.

  1. GOROOT is the directory where Go is installed, including its tools and standard library packages.
  2. GOPATH is 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/src directory. 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.

  1. Create a new directory for the project called testHello:

    cd ~/project
    mkdir testHello
  2. Change to the testHello directory:

    cd testHello
  3. Initialize the Go Module for this project:

    go mod init testHello

    The output should be:

    go: creating new go.mod: module testHello
  4. 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.

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:

  1. First, look at the structure of the helloWorld and test directories:

    helloWorld
    ├── go.mod
    └── helloWorld.go
    test
    ├── go.mod
    └── test.go
  2. 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

Importing Remote Packages

In Go, you can easily import remote packages. Let's demonstrate this by using a remote package from GitHub.

  1. Create a directory remoteModule:

    cd ~/project
    mkdir remoteModule
    cd remoteModule
  2. Initialize the Go Module:

    go mod init remoteModule
  3. 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.

  4. 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.

Mini Test

In this step, we will test our knowledge by creating a new file that uses the Hello function from the remote module.

  1. Create a folder remoteModule2:

    cd ~/project
    mkdir remoteModule2
    cd remoteModule2
  2. Initialize the Go Module:

    go mod init remoteModule2
  3. 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()
    }
  4. Run the program:

    go get github.com/labex-labs/golang-dev-code/chap02module
    go run remote2.go

    The output should be:

    hello
✨ Check Solution and Practice

Summary

In this lab, we learned the following:

  • The meaning and purpose of GOPATH and GOROOT directories.
  • 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.

Other Golang Tutorials you may like