Go

General

Installing Tool Dependencies

  • Create a file tools.go

    // This comment is here so that the "normal" build process ignores it.
    // To install these dependencies run:
    //
    // `go get -tags tools .`
    //
    // or simply
    //
    // `go mod tidy`
    
    //go:build tools
    
    // This allows us to have a "self contained" codebase,
    // there are not (a lot of) external tools required for this to work.
    
    //go:generate go install "google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest"
    //go:generate go install "google.golang.org/protobuf/cmd/protoc-gen-go@latest"
    
    package main
    
    import (
      _ "google.golang.org/grpc/cmd/protoc-gen-go-grpc"
      _ "google.golang.org/protobuf/cmd/protoc-gen-go"
    )
    
  • Run the following command

    go mod tidy
    go get -tags tools ./...
    go generate -tags tools
    

Source:

In your source code put this as the first line:

// +build tag_name

or (to include):

//go:build tag_name

alternatively (to exclude):

// +build !tag_name

Then build with:

go build -tags tag_name

Go experiments

NOTE The experiments are available via build tags, so you can build with:

as of Go 1.22 the rangefunc experiment is available

//go:build goexperiment.rangefunc

package example
GOEXPERIMENT=rangefunc go build

Getting a specific dependency version

Source

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:

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

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

$ go 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:

require example.com/theirmodule v1.3.4

Discovering available updates

Source:

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:

$ go list -m -u all

Display the latest version available for a specific module:

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

gRPC

Required Tools

buf

buf.build

We thought about Protocol Buffers so you don't have to.

Relative Imports in gRPC and buf

  • how does this work?

Bitbucket

Using BitBucket Server/Datacenter Git

  1. Create a personal access token

  2. Do this for your git configuration

    git config --global http.https://bitbucket.example.com.extraHeader 'Authorization: Bearer  Token'
    

    Some more details

    git clone -c http.extraHeader='Authorization: Bearer REPLACE_WITH_TOKEN' https://bitbucket.example.com/scm/project-name/repo-name.git
    git config --global http.https://bitbucket.example.com.extraHeader 'Authorization: Bearer  Token'
    

    OR (if you don't want to use the token in the header)

    git config --global url."https://<username>:<personal-access-token>@bitbucket.example.com".insteadOf "https://bitbucket.example.com"
    
  3. As your module URLs use something like this for Go projects:

    • bitbucket.example.com/project/repo

      Do NOT use the scm part in the URL


Children
  1. Customizing Go Binaries with Build Tags
  2. Managing Go Installations

Tags

  1. kb
  2. go
  3. grpc (Private)

Backlinks