Software Engineering / Golang¶
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
- Software Engineering / Golang
- Installation
- Links
- Decision Patterns
- Useful Packages
- Useful Scripts
- Dockerfile Recipes
- Makefile Recipes
- Useful Tools
- Community Events
Installation¶
Installation on Ubuntu¶
sudo add-apt-repository ppa:longsleep/golang-backports;
sudo apt update;
sudo apt install golang-go;
Links¶
Decision Patterns¶
- First class functions
- Support for native classes
- Lean keyword set
- Support for OOP
- Tuple returns
- Strongly typed language
- Type inference
- Compiled binaries
Useful Packages¶
Name | Description | URL |
---|---|---|
Cobra | CLI framework | https://github.com/spf13/cobra |
Gorilla Mux | HTTP request multiplexer | https://github.com/gorilla/mux |
Litter | console.log for Go |
https://github.com/sanity-io/litter |
Logrus | Logging | https://github.com/sirupsen/logrus |
UUID | Unique ID generator | https://github.com/google/uuid |
Testify | Testing framework | https://github.com/stretchr/testify |
Viper | Configuration management | https://github.com/spf13/viper |
Useful Scripts¶
System Administration¶
Environment Checking¶
go env
Development¶
Installing Dependencies¶
go mod vendor -v;
Updating Dependencies¶
go mod tidy -v;
Running Commands¶
go run ./cmd/command;
Running Tests¶
go test -cover -coverprofile c.out ./...;
Building¶
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.
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;
Dockerfile Recipes¶
The following assumes that the script make bin
will create a primary binary at ./bin/app
FROM golang:1.13-alpine3.10 AS base
ARG BIN_NAME="app"
RUN apk update --no-cache
RUN apk upgrade --no-cache
RUN apk add --no-cache make upx ca-certificates
WORKDIR /build
COPY . /build
RUN make bin
RUN upx /build/bin/${BIN_NAME} -o /build/bin/${BIN_NAME}_upxed
FROM scratch AS final
ARG BIN_NAME="app"
COPY --from=base /usr/share/ca-certificates /usr/share/ca-certificates
COPY --from=base /etc/passwd /etc/passwd
COPY --from=base /build/bin/${BIN_NAME}_upxed /app
ENTRYPOINT ["/app"]
Makefile Recipes¶
deps:
go mod vendor -v
go mod tidy -v
run:
go run ./cmd/$(CMD_ROOT)
test:
go test -v ./... -cover -coverprofile c.out
build:
CGO_ENABLED=0 \
go build \
-v \
-o ./bin/$(CMD_ROOT)_$$(go env GOOS)_$$(go env GOARCH)${BIN_EXT} \
./cmd/$(CMD_ROOT)
build_production:
CGO_ENABLED=0 \
go build \
-a -v \
-ldflags "-X main.Commit=$$(git rev-parse --verify HEAD) \
-X main.Version=$$(git describe --tag $$(git rev-list --tags --max-count=1)) \
-X main.Timestamp=$$(date +'%Y%m%d%H%M%S') \
-extldflags 'static' \
-s -w" \
-o ./bin/$(CMD_ROOT)_$$(go env GOOS)_$$(go env GOARCH)${BIN_EXT} \
./cmd/$(CMD_ROOT)
Useful Tools¶
Name | Description | Link |
---|---|---|
Go | Rich Go language support for Visual Studio Code | https://marketplace.visualstudio.com/items?itemName=ms-vscode.Go |