Go

General

Installing Tool Dependencies

  • Create a file tools.go

     1// This comment is here so that the "normal" build process ignores it.
     2// To install these dependencies run:
     3//
     4// `go get -tags tools .`
     5//
     6// or simply
     7//
     8// `go mod tidy`
     9
    10//go:build tools
    11
    12// This allows us to have a "self contained" codebase,
    13// there are not (a lot of) external tools required for this to work.
    14
    15//go:generate go install "google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest"
    16//go:generate go install "google.golang.org/protobuf/cmd/protoc-gen-go@latest"
    17
    18package main
    19
    20import (
    21  _ "google.golang.org/grpc/cmd/protoc-gen-go-grpc"
    22  _ "google.golang.org/protobuf/cmd/protoc-gen-go"
    23)
    
  • Run the following command

    1go mod tidy
    2go get -tags tools ./...
    3go generate -tags tools
    

Getting a specific dependency version

Source

  • Getting a specific dependency version (go.dev)

    You can get a specific version of a dependency module by specifying its version in the go get command. The command updates the require directive in your go.mod file (though you can also update that manually).

    You might want to do this if:

    You want to get a specific pre-release version of a module to try out. You’ve discovered that the version you’re currently requiring isn’t working for you, so you want to get a version you know you can rely on. You want to upgrade or downgrade a module you’re already requiring. Here are examples for using the go get command:

    To get a specific numbered version, append the module path with an @ sign followed by the version you want:

    1go get example.com/theirmodule@v1.3.4
    

    To get the latest version, append the module path with @latest:

    1go get example.com/theirmodule@latest
    

    The following go.mod file require directive example (see the go.mod reference for more) illustrates how to require a specific version number:

    Todo

    FIXME find out if go.mod lexer is available

    1# gomod
    2require example.com/theirmodule v1.3.4
    

Discovering available updates

Source:

  • Discovering available updates (go.dev)

    You can check to see if there are newer versions of dependencies you’re already using in your current module. Use the go list command to display a list of your module’s dependencies, along with the latest version available for that module. Once you’ve discovered available upgrades, you can try them out with your code to decide whether or not to upgrade to new versions. For more about the go list command, see go list -m.

    Here are a couple of examples.

    List all of the modules that are dependencies of your current module, along with the latest version available for each:

    1go list -m -u all
    

    Display the latest version available for a specific module:

    1go list -m -u example.com/theirmodule
    

gRPC