🌟
z's
  • Hello
  • Cheatsheets
    • 🍂Docker Compose Services
    • 🌿Git
    • ▶️ Golang
      • Gotchas
    • ⛑️Helm
    • ☸️ Kubernetes Management
    • ☸️ Kubernetes Resources
    • ☸️Kubernetes Snippets
    • 🔨Tools Quicklinks
    • Tools and Useful Stuff
    • 🟠Using Ubuntu
    • Reference/Template Dockerfiles
  • How-Tos
    • Use Ubuntu
    • Use VSCode
    • Use AWS
    • Use Git
    • Use GPG keys
    • Use Digital Ocean
  • About Me
    • Want to work with me?
    • How to work with me
  • Useful Tools
    • Collaboration
      • Miro
    • Documentation
      • Gitbook
      • Notion
  • On Growing People
    • Ontological Coaching
    • Organization Development (OD)
    • Speech Acts
    • Books & Other Resources
  • On Creating Software
    • Product
    • Design
    • Development Environments
      • Introduction
      • Visual Studio Code/Codium
      • Public Key Infrastructure (PKI) Setup & Usage
    • Patterns
      • API Authentication
      • User Authentication
    • Languages/Formats
      • JavaScript
      • Golang
      • HTML
      • CSS
      • SQL
      • JSON
      • YAML
    • Code Logistics
    • Data Persistence
      • Cassandra
    • Software Architecture
    • System Observability
    • Cool Tools
    • Kubernetes
      • Resource Cheatsheet
      • 1/ Kubernetes in 5 Minutes
      • 2/ Setting up Kubernetes locally
      • 3/ Handling long-running workloads
      • 4/ Handling run-once workloads
Powered by GitBook
On this page
  • Installation on Ubuntu
  • System Checks
  • Development
  • Installing Dependencies
  • Updating Dependencies
  • Running Commands
  • Running All Tests
  • Running All Tests in a Directory
  • Running All Tests with Coverage
  • Building
  • Basic Build
  • Static Build
  • Symbols Stripped Build
  • Cross-Platform Build
  • Variable Injection Build
  • Windows-GUI Build

Was this helpful?

  1. Cheatsheets

▶️ Golang

Installation on Ubuntu

sudo add-apt-repository ppa:longsleep/golang-backports;
sudo apt update;
sudo apt install golang-go;

System Checks

Environment Checking

go env

Development

Installing Dependencies

go mod vendor -v;

Updating Dependencies

go mod tidy -v;

Running Commands

go run ./cmd/command;

Running All Tests

go test ./...

Running All Tests in a Directory

go test ./path/to/directory/...

Running All Tests with Coverage

go test -cover -coverprofile c.out ./...;

Building

Basic Build

go build -o ./bin/command ./cmd/command

Static Build

CGO_ENABLED=0 \
  go build \
  -ldflags "-extldflags 'static'" \
  -o ./bin/command \
  ./cmd/command;

Symbols Stripped Build

go build \
  -ldflags "-s -w" \
  -o ./bin/command \
  ./cmd/command;

Cross-Platform Build

GOOS=windows GOARCH=386 \
  go build \
  -o ./bin/command \
  ./cmd/command;

Variable Injection Build

The following assumes the presence of the variables commitHash, version, and buildTimestamp in the main package:

go build \
  -ldflags "-X main.commitHash=$(git rev-parse --verify HEAD) \
    -X main.version=$(git describe --tag $(git rev-list --tags --max-count=1)) \
    -X main.buildTimestamp=$(date +'%Y%m%d%H%M%S')" \
  -o ./bin/command \
  ./cmd/command;

Windows-GUI Build

GOOS=windows GOARCH=386 \
  go build \
  -ldflags '-H=windowsgui' \
  -o ./bin/command \
  ./cmd/command;
PreviousGitNextGotchas

Last updated 4 years ago

Was this helpful?

The following formulae expects that an invoker runs these from the root directory given a project layout as specificied at .

https://github.com/golang-standards/project-layout