Skip to content

Instantly share code, notes, and snippets.

@aint
Last active December 13, 2025 21:13
Show Gist options
  • Select an option

  • Save aint/282bdd8a8c3198355536a6f49d6dd3c7 to your computer and use it in GitHub Desktop.

Select an option

Save aint/282bdd8a8c3198355536a6f49d6dd3c7 to your computer and use it in GitHub Desktop.
Golang recipes

https://github.com/nikolaydubina/go-recipes

Make graph of dependent modules with modgraphviz

go mod graph | modgraphviz | dot -Tsvg -o mod-graph.svg

Requires:

https://graphviz.org/download/
go install golang.org/x/exp/cmd/modgraphviz@latest

Get Go versions of dependent modules

go list -deps -json ./... | jq -rc 'select(.Standard!=true and .Module.GoVersion!=null) | [.Module.GoVersion,.Module.Path] | join(" ")' | sort -V | uniq

Get directly dependent modules that can be upgraded

go list -u -m $(go list -m -f '{{.Indirect}} {{.}}' all | grep '^false' | cut -d ' ' -f2) | grep '\['

Locally patch dependency with replace

https://eli.thegreenplace.net/2024/locally-patching-dependencies-in-go

# clone your dependency to $DEP folder
# make changes
go mod edit -replace github.com/google/go-cmp=$DEP

Enum via generics

https://github.com/orsinium-labs/enum

type Color enum.Member[string]

var (
  Red    = Color{"red"}
  Green  = Color{"green"}
  Blue   = Color{"blue"}
  Colors = enum.New(Red, Green, Blue)
)

Include metadata in binary during compilation with ldflags

You can pass metadata through compiler to your binary. This is useful for including things like git commit, database schema version, integrity hashes. Variables can only be strings.

go build -v -ldflags="-X 'main.Version=v1.0.0'"
go build -v -ldflags="-X 'my/pkg/here.Variable=some-string'"
package main

var Version string

func main() {
  // Version here has some value
  ...
}

Monitor Go runtime metrics in browser

https://github.com/arl/statsviz

Detect potential Nil panics

https://github.com/uber-go/nilaway

Better structured concurrency for go

https://github.com/sourcegraph/conc

Swiss army knife library

https://github.com/duke-git/lancet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment