How to Install Go on Ubuntu 20.04
Reference: https://linuxize.com/post/how-to-install-go-on-ubuntu-20-04/
Golang Tutorial Using Visual Studio Code
Reference: https://www.bogotobogo.com/GoLang/GoLang_Visual_Studio_Code.php
Create A Go Module
cd ~/project/go-course/hello
go mod init hello
Build Go Program/Module
Use “go build”. If it errors like below, it means you haven’t initialized your folder as a Go module.
ceefour@amanah:~/go/src/hello$ go build
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
Run Go Program
From Terminal:
go run hello
From VS Code, choose Run > Run Without Debugging or press Ctrl+F5. Alternatively, right click file and choose Run Code, or Alt+Ctrl+N.
Debug Go Program
From VS Code, choose Run > Start Debugging or press F5.
Frameworks & Libraries
Web framework / REST API: Echo
Mocking framework: GoMock
Unit test: Unit tests in Go
MongodB: mongo-go-driver
Go Language Resources / Books
Using Go Modules
Reference:
List The Current Module and All Its Dependencies
Command: go list -m all
$ go list -m all
(CURRENT MODULE)
cloud.google.com/go v0.26.0
github.com/BurntSushi/toml v0.3.1
github.com/DataDog/datadog-go v4.4.0+incompatible
github.com/DataDog/gostackparse v0.5.0
github.com/DataDog/sketches-go v1.0.0
github.com/Microsoft/go-winio v0.5.0
...
Using Makefiles
Reference: Makefiles with Go | golangdocs.com
Golang projects often use Makefiles to perform build management tasks such as installing dependencies, building, testing, etc.
Typical Makefile tasks for Golang projects:
make installdep
: Install dependencies. This runsgo get
andgo mod
commands.<code>make
cleandep: Add missing and remove unused modules, usinggo mod tidy
.<code>make
build-image: Build Docker container image.<code>make
test: Run code coverage and unit tests usinggo test
. Code coverage is typically powered by Sonar, specified insonar-project.properties
file.<code>make
install: Download modules to local cache usinggo mod download
. This is similar tomake installdep
, but does not make vendored copy of dependencies.<code>make
build: Build the Go project, but does not create a Docker container image.