github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/www/docs/cookbooks/build-go-modules.md (about) 1 # Building Go modules 2 3 With the default configs, you can already build a Go module without issues. 4 5 But, if you want to access module information in runtime (e.g. `debug.BuildInfo` or `go version -m $binary`), you'll 6 need to setup GoReleaser to "proxy" that module before building it. 7 8 To do that, you can simply add this to your config: 9 10 ```yaml 11 # goreleaser.yml 12 gomod: 13 proxy: true 14 ``` 15 16 In practice, what this does is: 17 18 - for each of your builds, create a `dist/proxy/{{ build.id }}`; 19 - creates a `go.mod` that requires your __main module__ at the __current tag__; 20 - creates a `main.go` that imports your __main package__; 21 - copy the project's `go.sum` to that folder. 22 23 In which: 24 25 - __build.id__: the `id` property in your `build` definition; 26 - __main module__: is the output of `go list -m`; 27 - __main package__: is the __main module__ + your build's `main`; 28 - __current tag__: is the tag that is being built. 29 30 So, let's say: 31 32 - __main module__: `github.com/goreleaser/nfpm/v2`; 33 - build's `main`: `./cmd/nfpm/`; 34 - __current tag__: `v2.5.0`. 35 36 GoReleaser will create a `main.go` like: 37 38 ```go 39 // +build: main 40 package main 41 42 import _ "github.com/goreleaser/nfpm/v2/cmd/nfpm" 43 ``` 44 45 a `go.mod` like: 46 47 ``` 48 module nfpm 49 50 require github.com/goreleaser/nfpm/v2 v2.5.0 51 ``` 52 53 Then, it'll run: 54 55 ```sh 56 go mod tidy 57 ``` 58 59 And, to build, it will use something like: 60 61 ```shell 62 go build -o nfpm github.com/goreleaser/nfpm/v2/cmd/nfpm 63 ``` 64 65 This will resolve the source code from the defined module proxy using `proxy.golang.org`. 66 Your project's `go.sum` will be used to verify any modules that are downloaded, with `sum.golang.org` "filling in" any gaps. 67 68 ## Getting the version from the module 69 70 You can also get the module version at runtime using [`debug#ReadBuildInfo`](https://pkg.go.dev/runtime/debug#ReadBuildInfo). 71 It is useful to display the version of your program to the user, for example. 72 73 ```golang 74 package main 75 76 import ( 77 "fmt" 78 "runtime/debug" 79 ) 80 81 func main() { 82 if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" { 83 fmt.Println(info) 84 } 85 } 86 ``` 87 88 You can also use `go version -m my_program` to display the go module information. 89 90 ## Limitations 91 92 1. Extra files will still be copied from the current project's root folder and not from the proxy cache; 93 1. You can't build packages that are not contained in the main module. 94 95 ## More information 96 97 You can find more information about it on the [issue][issue] that originated it and its subsequent [pull request][pr]. 98 99 Make sure to also read the [relevant documentation][docs] for more options. 100 101 [issue]: https://github.com/goreleaser/goreleaser/issues/1354 102 [pr]: https://github.com/goreleaser/goreleaser/pull/2129 103 [docs]: /customization/gomod/ 104 105 ## Real example 106 107 Source code of a working example can be found at [goreleaser/example-mod-proxy](https://github.com/goreleaser/example-mod-proxy).