Package Deployment

How code is packaged, deployed and reused across different projects, libraries and applications.

Go Package Deployment


Compile

Basics of how to compile and execute a basic program and other common tools.

Error Loading

Missing

To compile and execute a basic Go program, use the go build and go run commands Example: Basic Go Compilation:

// main.go package main import "fmt" func main() { fmt.Println("Hello, Go!") }

To compile and run:

go build -o myprogram main.go ./myprogram

Alternatively, you can directly execute the Go program without compiling:

go run main.go

Common Compilation Tools:

  • go fmt – Formats Go code according to Go standards.
  • go vet – Analyzes code for potential issues.
  • golint – Provides stylistic recommendations.
go fmt main.go go vet main.go golint main.go

Modules

Create a reusable module in the local project. Manage exports and imports into a consuming module.

Missing

Creating a Reusable Module: Go modules allow you to manage dependencies and organize reusable code.

Initialize a Module:

go mod init example.com/mymodule

This creates a go.mod file:

module example.com/mymodule go 1.20

To defining an Exported Function:

// mymodule/mylib.go package mymodule import "fmt" // Exported function (must start with uppercase letter) func SayHello(name string) { fmt.Printf("Hello, %s!\n", name) }

Using the Module in Another File:

package main import ( "example.com/mymodule" ) func main() { mymodule.SayHello("Alice") }

Managing Dependencies:

  • Add a dependency: go get example.com/somepackage
  • Remove unused dependencies: go mod tidy
  • List dependencies: go list -m all

Import Package

Add an external package to a program and import it. Upgrade to new revisions and lock to a specific version.

Missing

To add an External Package. Use go get to install third-party packages.

Example: Using github.com/fatih/color for Colored Output:

go get github.com/fatih/color

Import and use it:

package main import ( "github.com/fatih/color" ) func main() { color.Red("This is a red message") }

Upgrading a Package:

go get -u github.com/fatih/color

Locking to a Specific Version:

go get github.com/fatih/color@v1.12.0

Checking and Managing Dependencies:

Go provides mechanisms to lock dependencies to specific versions and ensure reproducible builds.

Generating and Committing a Lockfile - Go automatically maintains a go.sum file, which records cryptographic checksums of dependencies. This file should be committed to version control to ensure consistency across builds.

git add go.sum git commit -m "Add go.sum to track dependency checksums"

Checking Installed Modules:

go list -m all # List all modules in use

Tidying Dependencies:

go mod tidy # Remove unused dependencies and update go.mod and go.sum

Verifying Module Integrity:

go mod verify # Verify checksums in go.sum against downloaded modules
go list -m all # List all modules go mod tidy # Remove unused dependencies go mod verify # Verify checksums

Publish a Package

Author and publish a package for external consumption.

Missing

Publishing a Go Package for External Use. To publish a package:

  1. Create a public repository on GitHub/GitLab.
  2. Ensure a valid go.mod file.
  3. Push the module to a repository.
git init git remote add origin https://github.com/username/mymodule.git git add . git commit -m "Initial commit" git push -u origin main

Tagging a Release:

git tag v1.0.0 git push origin v1.0.0

Using the Published Module:

go get github.com/username/mymodule@v1.0.0

Example: Importing and Using the Published Module:

package main import ( "github.com/username/mymodule" ) func main() { mymodule.SayHello("Bob") }

Tests

Add tests to a package that can be run as part of CI/CD.

Missing

Adding Tests to a Package. Go includes a built-in testing package for unit testing. Example: Writing a Test:

// mymodule/mylib.go package mymodule func Add(a, b int) int { return a + b }
// mymodule/mylib_test.go package mymodule import "testing" func TestAdd(t *testing.T) { result := Add(2, 3) expected := 5 if result != expected { t.Errorf("Expected %d but got %d", expected, result) } }

Running Tests:

go test ./...

Running Tests with Coverage:

go test -cover ./...

Integrating Tests into CI/CD. To automate testing in CI pipelines, add:

go test ./... && go vet ./...

to your build pipeline configuration.


Custom Tools

Incorporate custom tools as part of the build process (e.g., OpenAPI code generation).

Missing

Incorporating Code Generation Tools. Some Go projects require automatic code generation, such as OpenAPI or gRPC.

Installing stringer for Enum String Generation

go install golang.org/x/tools/cmd/stringer@latest

Using it in Go:

// Define an enum package main //go:generate stringer -type=Animal type Animal int const ( Dog Animal = iota Cat Fish )

Generate code:

go generate ./...

Generating OpenAPI with oapi-codegen:

go install github.com/deepmap/oapi-codegen/cmd/oapi-codegen@latest oapi-codegen -generate types -o types.gen.go api.yaml

Automating Builds with a Custom Script. Example build.sh script:

#!/bin/bash set -e go fmt ./... go vet ./... go test ./... go build -o myapp main.go echo "Build successful!"

Run:

chmod +x build.sh ./build.sh