😀
Notes
  • My Notes
  • Software Development
    • Getting Started
    • VSCodium
    • Go
  • System Administration
    • Networking cheatsheet
    • Infra security check tools
    • Using Ubuntu as a workstation
  • Application Infrastructure
    • Message Brokers
      • Kafka
      • NATS
    • Databases
      • MongoDB
      • MySQL
      • PostgreSQL
      • Redis
    • Kubernetes
      • Standard resources cheatsheet
      • Istio
      • Prometheus
    • Workflow Orchestrators
      • Airflow
  • Cloud Infrastructure
    • Terraform
      • AWS
        • Kubernetes IAM roles
  • Climbing
    • Overview of Climbing
    • Singapore
  • Crypto
    • Introduction to Crypto
    • Web3 terminology
  • Guides
    • Beginner's Guide to Personal Operational Security
Powered by GitBook
On this page
  • Cheatsheet/References
  • Dockerfile
  • Makefile
  • Useful packages
  1. Software Development

Go

PreviousVSCodiumNextNetworking cheatsheet

Last updated 1 year ago

Cheatsheet/References

The following have now been implemented in a GitHub repository that can be found at:

Dockerfile

Pair the following with the Makefile below

ARG GO_VERSION=1.21
FROM golang:${GO_VERSION}-alpine AS build
RUN apk add --no-cache ca-certificates git g++ make
WORKDIR /go/src/app
COPY ./go.mod ./go.sum ./
RUN go mod download -x
COPY ./*.go ./
COPY ./cmd ./cmd
COPY ./internal ./internal
ENV CGO_ENABLED=0
RUN make install-swaggo-ci
RUN make docs-swaggo
RUN make deps
RUN make binary
RUN sha256sum ./bin/app > ./bin/app.sha256

FROM scratch AS final
COPY --from=build /go/src/app/bin/app /app
COPY --from=build /go/src/app/bin/app.sha256 /app.sha256
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
ENTRYPOINT ["/app"]

Makefile

This Makefile contains generic-use recipes for a standard Go project

APP_NAME := app
BIN_PATH := ./bin
CMD_PATH := ./cmd
IMAGE_PATH := ${APP_NAME}/${APP_NAME}
IMAGE_REGISTRY := docker.io
IMAGE_TAG := latest

# everything before this next line is configurable in the Makefile.properties
-include Makefile.properties
# everything after this line is a derived value from the above variables
IMAGE_URL := ${IMAGE_REGISTRY}/${IMAGE_PATH}

ifeq ("${GOOS}", "windows")
BINARY_EXT := ".exe"
endif

binary:
	@echo "building binary for ${APP_NAME} for os/arch $$(go env GOOS)/$$(go env GOARCH)..."
	@mkdir -p "${BIN_PATH}"
	@go build \
		-ldflags "\
			-extldflags 'static' -s -w \
			-X ${APP_NAME}/internal/constants.AppName=${APP_NAME} \
			-X ${APP_NAME}/internal/constants.BuildTimestamp=$$(date --utc +'%Y-%m-%dT%H:%M:%S') \
			-X ${APP_NAME}/internal/constants.Version=$$(git rev-parse --abbrev-ref HEAD)-$$(git rev-parse HEAD | head -c 6) \
		" \
		-o "${BIN_PATH}/${APP_NAME}${BINARY_EXT}" \
		"${CMD_PATH}/${APP_NAME}"
	@cp "${BIN_PATH}/${APP_NAME}${BINARY_EXT}" "${BIN_PATH}/${APP_NAME}_$$(go env GOOS)_$$(go env GOARCH)${BINARY_EXT}"

deps:
	@echo "updating dependencies..."
	@go mod tidy
	@go mod vendor

docs-swaggo: 
	@echo "generating documentation package..."
	swag init \
		--generalInfo "./internal/api/api.go" \
		--output "${DOCS_OUTPUT_PATH}" \
		--parseInternal \
		--parseDependency \
		--parseDepth 8

image:
	@echo "building image ${IMAGE_URL}:${IMAGE_TAG}..."
	docker build -t ${IMAGE_URL}:${IMAGE_TAG} .

test:
	@echo "running tests..."
	@go test -v -coverpkg=./... -coverprofile=./tests/cover.out ./...
	@go tool cover -func ./tests/cover.out
	@go tool cover -html ./tests/cover.out -o ./tests/cover.html

install-swaggo:
	@echo "installing swag from ${SWAGGO_URL}..."
	go get -u ${SWAGGO_URL}/cmd/swag@latest

install-swaggo-ci:
	@echo "installing swag from ${SWAGGO_URL}..."
	go install ${SWAGGO_URL}/cmd/swag@latest

Useful packages

Name
Description

cobra

CLI structure

gofiber

HTTP server

logrus

Logger utilities tool

viper

Configuration management

GitHub - zephinzer/template-go-service: Template for a Go service that has HTTP server, worker, and job components. Includes local setups for multiple databases via Docker Compose and deployment manifests for Kubernetes via HelmGitHub
Logo