From 41356ea84d734928493a7342d3c44e4b163237bc Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 5 Apr 2021 11:43:17 -0300 Subject: [PATCH] docs: improve go mod proxy docs Signed-off-by: Carlos Alexandro Becker --- www/docs/cookbooks/build-go-modules.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/www/docs/cookbooks/build-go-modules.md b/www/docs/cookbooks/build-go-modules.md index 1cd8471b8..3fba6713f 100644 --- a/www/docs/cookbooks/build-go-modules.md +++ b/www/docs/cookbooks/build-go-modules.md @@ -65,6 +65,28 @@ go build -o nfpm github.com/goreleaser/nfpm/v2/cmd/nfpm This will resolve the source code from the defined module proxy using `proxy.golang.org`. Your project's `go.sum` will be used to verify any modules that are downloaded, with `sum.golang.org` "filling in" any gaps. +## Getting the version from the module + +You can also get the module version at runtime using [`debug#ReadBuildInfo`](https://pkg.go.dev/runtime/debug#ReadBuildInfo). +It is useful to display the version of your program to the user, for example. + +```golang +package main + +import ( + "fmt" + "runtime/debug" +) + +func main() { + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" { + fmt.Println(info) + } +} +``` + +You can also use `go version -m my_program` to display the go module information. + ## Limitations 1. Extra files will still be copied from the current project's root folder and not from the proxy cache;