github.com/goreleaser/goreleaser@v1.25.1/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.yaml
    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  - copy the project's `go.sum` to that directory.
    21  - runs `go get module@version`
    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 id**: `nfpm`
    34  - build's `main`: `./cmd/nfpm/`;
    35  - **current tag**: `v2.5.0`.
    36  
    37  GoReleaser will create a `go.mod` like:
    38  
    39  ```gomod
    40  module nfpm
    41  ```
    42  
    43  Then it'll copy the `go.sum` into this directory, and run:
    44  
    45  ```sh
    46  go get github.com/goreleaser/nfpm/v2@v2.5.0
    47  ```
    48  
    49  And, to build, it will use something like:
    50  
    51  ```shell
    52  go build -o nfpm github.com/goreleaser/nfpm/v2/cmd/nfpm
    53  ```
    54  
    55  This will resolve the source code from the defined module proxy using `proxy.golang.org`.
    56  Your project's `go.sum` will be used to verify any modules that are downloaded, with `sum.golang.org` "filling in" any gaps.
    57  
    58  ## Getting the version from the module
    59  
    60  You can also get the module version at runtime using [`debug#ReadBuildInfo`](https://pkg.go.dev/runtime/debug#ReadBuildInfo).
    61  It is useful to display the version of your program to the user, for example.
    62  
    63  ```golang
    64  package main
    65  
    66  import (
    67  	"fmt"
    68  	"runtime/debug"
    69  )
    70  
    71  func main() {
    72    if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
    73      fmt.Println(info)
    74    }
    75  }
    76  ```
    77  
    78  You can also use `go version -m my_program` to display the go module information.
    79  
    80  ## Limitations
    81  
    82  1. Extra files will still be copied from the current project's root directory and not from the proxy cache;
    83  1. You can't build packages that are not contained in the main module;
    84  1. VCS info will not be available.
    85  
    86  ## More information
    87  
    88  You can find more information about it on the [issue][issue] that originated it and its subsequent [pull request][pr].
    89  
    90  Make sure to also read the [relevant documentation][docs] for more options.
    91  
    92  [issue]: https://github.com/goreleaser/goreleaser/issues/1354
    93  [pr]: https://github.com/goreleaser/goreleaser/pull/2129
    94  [docs]: /customization/verifiable_builds/
    95  
    96  ## Real example
    97  
    98  Source code of a working example can be found at [goreleaser/example-mod-proxy](https://github.com/goreleaser/example-mod-proxy).