Go dev-environment on Ubuntu

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

Creates a standard directory structure for a Go project.
				
					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

In Golang, a module is the basic unit for managing program dependencies. The module system was introduced in Go 1.11 and provides much more convenient and powerful dependency management than the previous GOPATH-based project structure. Modules help prevent dependency conflicts, specify versions, and allow multiple developers to work consistently on the same codebase.
* Key concepts of the module
1. Module : A module is a collection of related packages and is managed through a go.mod file. Each module has a unique path and version and is often version-controlled (e.g., with Git).
2. go.mod File : This file defines the module’s configuration and dependencies. When you initialize a Go module, a go.mod file is created, specifying the module name, Go version, and any external dependencies with their versions.
3. go.sum File : This file contains checksums of the dependencies listed in go.mod. It ensures the integrity of dependencies by verifying that their versions remain unchanged.
4. Package : Modules contain packages, which are sub-units of code organized into directories. You can import packages to use in other modules or packages.
This explains how to manage modules locally without using external sources (e.g. GitHub). Go modules natively support both local and remote modules, so using local modules is very simple. Initialize a Go module in the root directory of your project. This sets the module path to be used throughout your project.
				
					user:~$ cd ~/workspace/myapp
user:~$ go mod init myapp
				
			
A go.mod file is created in the project root (~/workspace/myapp/go.mod).

6. Create internal package

Let’s create an example of creating an internal package in the internal directory. The necessary functionality can be created using this method.
				
					user:~$ mkdir -p internal/util
				
			
Create internal/util/util.go file.
				
					package util

import "fmt"

// SayHello prints a greeting message
func SayHello(name string) {
    fmt.Printf("Hello, %s!\n", name)
}
				
			

7. Create a main package

Create cmd/main.go file.
				
					package main

import "myapp/internal/util"

func main() {
    util.SayHello("Go Developer")
}
				
			
import “myapp/internal/util” refers to the internal package based on the module name (myapp) set in the go.mod file. This is why the module initialization was done in the project root.

8. Run and Compile

You can run the program immediately without compile.
				
					user:~$ cd ~/workspace/myapp
user:~/workspace/myapp$ go run ./cmd
				
			
You can build the program by compiling it. This will create an executable binary named myapp in the bin directory.
				
					user:~/workspace/myapp$ go build -o bin/myapp ./cmd
				
			
You can run the built executable binary.
				
					user:~/workspace/myapp$ bin/myapp
				
			

9. Using Makefile

You can use Makefiles to automate various tasks such as building, testing, and cleaning.
Create ~/workspace/myapp/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)

				
			
* How to use:
   > Build: make build
   > Run: make run
   > Clean: make clean

Leave a Reply

Your email address will not be published. Required fields are marked *