Contents

go

Starter Template

I saved this as a snippet for vscode to get up and running quickly with something better than the defaults for handling func main isolation. I’ve been working on modifying this a bit as I don’t really like using args, but am trying not to overcomplicate things as a new gopher.

I tend to like better flag parsing than using args, but it’s still a better pattern to get functions isolated from main to easily test.

The gist that I’ve taken from this and discussions in the community is ensure that main is where program termination is dedicated instead of handling this in your functions. This isolation of logic from main ensures you can more easily setup your tests as well, since func main() isn’t testable.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main

// package template from:
import (
    "errors"
    "fmt"
    "io"
    "os"
)

const (
    // exitFail is the exit code if the program
    // fails.
    exitFail = 1
)

func main() {
    if err := run(os.Args, os.Stdout); err != nil {
        fmt.Fprintf(os.Stderr, "%s\n", err)
        os.Exit(exitFail)
    }
}

func run(args []string, stdout io.Writer) error {
    if len(args) == 0 {
        return errors.New("no arguments")
    }
    for _, value := range args[1:] {
        fmt.Fprintf(stdout, "Running %s", value)
    }
    return nil
}

Pre-Commit

Here’s how to setup pre-commit for Go projects.

  1. Install pre-commit for macOS: brew install pre-commit or see directions for curl/other options for WSL, Windows, Linux, etc.
  2. Use the template from TekWizely/pre-commit-golang: Pre-Commit hooks for Golang with support for Modules
    1. Several options are provided for fmt oriented commands. Comment out any duplicates that don’t apply.
  3. Finally initialize the pre-commit hooks in your repo by running: pre-commit install

Validate everything is working by running: pre-commit run --all-files

Periodically, you can run pre-commit autoupdate to ensure the latest version of the pre-commit hooks are upgraded.

Logging

At this stage, I’m using zerolog as I found it very easy to get moving with structured logs.

The output of this demo looks pretty good!

https://d33wubrfki0l68.cloudfront.net/8b939ddf9947644bc2c4a9c0c00fc80375e06132/88608/images/r1-d014-structured-console-output.png

Here’s a functional demo that can be used to bootstrap a new project with this.

Code Coverage Report

original post: 1

Use gopherbadge2

1
go install github.com/jpoles1/[email protected]
  • Set ![gopherbadger-tag-do-not-edit]() in the readme, and then this gets replaced with a code coverage percentage badge.
  • Generate the required code coverage reports using:
1
2
3
go test ./... -coverprofile ./artifacts/cover.out
go tool cover -html=./artifacts/cover.out -o ./artifacts/coverage.html
gopherbadger -md="README.md,coverage.md" -tags 'unit'

Repos


  1. go-r1-day-41 ↩︎

  2. GitHub - jpoles1/gopherbadger: Generate coverage badge images using Go! ↩︎


Webmentions

(No webmentions yet.)