This is a step-by-step guide to configuring a development project using Go modules locally without any external dependencies. In the example for explanation, we will create a project directory under the ~/workspace directory locally and proceed with development.
1. Install Go
Update the package list of your system to the latest version, install Go, and then complete the installation by checking that the installation was successful.
user:~$ sudo add-apt-repository ppa:longsleep/golang-backports
user:~$ sudo apt update
user:~$ sudo apt install golang
user:~$ go version
2. Create a working directory
Create a working directory for Go development. The project uses Go modules.
This method allows for more flexible project management than the traditional GOPATH structure. The module concept will be explained in detail as we configure the project.
user:~$ mkdir -p ~/workspace
3. Go Environment Settings
Sets the environment variables required for Go development. GOPATH is not required when using Go modules, but may be required when using some tools or packages.
Set the environment in ~/.bashrc so that the settings can be applied to the system even when rebooting. Register the Go environment variable at the end of the configuration file with an editor. Then apply .bashrc using the source command. It will be automatically applied when rebooting.
user:~$ vi ~/.bashrc
# Go Env variables
export GOPATH=$HOME/workspace
export PATH=$PATH:$GOPATH/bin
user:~$ source ~/.bashrc
- GOPATH specifies the path to your Go workspace.
- By adding GOPATH/bin to your PATH, you can run binaries installed with Go directly from the terminal.
- The Go binary installed with apt is likely already included in your system PATH, so you only need to add GOPATH/bin to your PATH.
4. Create a project structure
user:~$ mkdir -p ~/workspace/myapp/{bin,cmd,internal,pkg,configs,scripts}
- bin/: Directory where built executables are stored.
- cmd/: Stores the entry point of your application (e.g. the main package).
- internal/: Stores internal packages and is not accessible from outside.
- pkg/: Stores reusable packages.
- configs/: Stores configuration files.
- scripts/: Stores scripts for builds, deployments, etc.
5. Initialize Local Go Module
go.mod
file. Each module has a unique path and version and is often version-controlled (e.g., with Git).go.mod
file is created, specifying the module name, Go version, and any external dependencies with their versions.go.mod
. It ensures the integrity of dependencies by verifying that their versions remain unchanged.
user:~$ cd ~/workspace/myapp
user:~$ go mod init myapp
6. Create internal package
user:~$ mkdir -p internal/util
package util
import "fmt"
// SayHello prints a greeting message
func SayHello(name string) {
fmt.Printf("Hello, %s!\n", name)
}
7. Create a main package
package main
import "myapp/internal/util"
func main() {
util.SayHello("Go Developer")
}
8. Run and Compile
user:~$ cd ~/workspace/myapp
user:~/workspace/myapp$ go run ./cmd
user:~/workspace/myapp$ go build -o bin/myapp ./cmd
user:~/workspace/myapp$ bin/myapp
9. Using Makefile
BINARY_NAME=myapp
BUILD_DIR=bin
.PHONY: all build run clean
all: build
build:
go build -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd
run: build
$(BUILD_DIR)/$(BINARY_NAME)
clean:
rm -f $(BUILD_DIR)/$(BINARY_NAME)