🌟
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
  • TL;DR
  • Why use it
  • Notable OSS projects
  • Dependency Management
  • Script Management
  • File Management
  • For importing by others
  • For creating a binary
  • Testing
  • File Organisation
  • Unit Testing
  • Coverage
  • Building
  • Go Generators
  • Building without CGO
  • Building with CGO
  • Static compilation
  • Build-time variable injection
  • Symbol stripping
  • Release Management

Was this helpful?

  1. On Creating Software
  2. Languages/Formats

Golang

One-pager for Go

TL;DR

Go (or Golang) is a strongly-typed programming language with C-styled semantics created by engineers from Google for server-side applications

Why use it

  1. You're building a backend system that should be efficient

  2. You want to implement concurrency and do it with style (nice language semantics)

  3. You want to distributable your code as a native binary

  4. You find Java and C# and friends too verbose/boring

  5. You find C++ too complicated and traditional

  6. You want a strongly-typed language that's not too verbose (maybe also see Kotlin/Rust)

  7. Your primary expertise is in cloud-native infrastructure and you're looking for a programming language you can apply in your system

  8. You're building something with custom logic to integrate with Kubernetes that YAML cannot do alone

Notable OSS projects

  1. Kubernetes

  2. Docker

  3. Terraform

  4. Istio (control plane)

  5. Prometheus

Dependency Management

Go comes with its own dependency management tool built into a sub-command. To initialise a Go project, run:

# go mod init github.com/path/to/your/repo

To install a dependency locally in your project, reference it in your import statement, prefix the import with an underscore so it doesn't get removed by go fmt, and run:

# go mod vendor

To install a dependency on your machine, run:

# go get github.com/path/to/module

For versions beyond version 1 (check the Git tags on the repository), the convention is:

# go get github.com/path/to/module.v2

If you want to upgrade the Go packages on your machine (not your local project):

# go get -u github.com/path/to/module

If you want to install your local project dependencies to your machine, run:

# go mod download

If the go.mod and go.sum file gets messed up somehow, run:

# go mod tidy

Script Management

There's no official script manager but typically Go projects use a Makefile to handle project-related tasks.

File Management

For importing by others

You normally want all your .go files in the root directory so it's easy for consumers to import it.

For creating a binary

You normally keep your root directory clean. The binary names should be /cmd relative to project root. Locally used packages that are not remotely destined for extraction into their own packages should be in /internal and packages that might be extracted eventually can go into /pkg.

Testing

File Organisation

Test files should be in the same directory as the code they test. For example:

/internal
  /somepkg
    /fn_one.go
    /fn_one_test.go
    /fn_two.go
    /fn_two_test.go

Unit Testing

To run all tests in your project, run:

# go test ./...

The ./... indicates all files recursively. You could also specify an exact path to a package like:

# go test ./internal/somepkg

Coverage

Go's test coverage report is weird. It does not conform to commonly found formats.

To generate the test coverage, run the test command with -cover -coverprofile c.out appended:

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

Note also that packages without any test files will not be considered in the overall coverage.

Building

Go Generators

Building without CGO

Building with CGO

Static compilation

Build-time variable injection

Symbol stripping

Release Management

Package releases are done using Git tags.

# git tag v1.0.2
# git push origin master

PreviousJavaScriptNextHTML

Last updated 4 years ago

Was this helpful?

This section covers how to organise files in a Go project. An excellent project with some conventions can be found at . The following elaborates based on their directory structure.

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