Skip to content

Add or remove a module in kubeturbo code

Kevin Wang edited this page Mar 29, 2022 · 1 revision

Background

Kubeturbo is using go modules to manage the module dependencies. Go modules were added since go 1.1.3, in response to a growing need to make it easier for developers to maintain various versions of their dependencies. Go modules commonly consist of one project or library and contain a collection of Go packages that are then released together. Go modules solve many problems with GOPATH, the original system, by allowing users to put their project code in their chosen directory and specify versions of dependencies for each module.

Add an external module as a dependency

Go modules are distributed from version control repositories, commonly Git repositories. When you want to add a new module as a dependency to your own, you use the repository’s path as a way to reference the module you’d like to use. When Go sees the import path for these modules, it can infer where to find it remotely based on this repository path.

For this example, you’ll add a dependency on the github.com/spf13/cobra library to your module.

go get github.com/spf13/cobra

When you run this command, the go tool will look up the Cobra repository from the path you specified and determine which version of Cobra is the latest by looking at the repository’s branches and tags. It will then download that version and keep track of the one it chose by adding the module name and the version to the go.mod file for future reference.

module github.com/turbonomic/kubeturbo

go 1.16

require (
	github.com/inconshreveable/mousetrap v1.0.0 // indirect
	github.com/spf13/cobra v1.2.1 // indirect
	github.com/spf13/pflag v1.0.5 // indirect
)

A new section using the required directive has been added. This directive tells Go which module you want, such as github.com/spf13/cobra, and the version of the module you added. Sometimes require directives will also include an // indirect comment. This comment says that, at the time the required directive was added, the module is not referenced directly in any of the module’s source files. A few additional require lines were also added to the file. These lines are other modules Cobra depends on that the Go tool determined should be referenced as well.

Using a specific version of a module

Since Go modules are distributed from a version control repository, they can use version control features such as tags, branches, and even commits. You can reference these in your dependencies using the @ symbol at the end of the module path along with the version you’d like to use.

go get github.com/spf13/cobra@latest

go get github.com/spf13/cobra@branch_name

go get github.com/spf13/cobra@tag_name

go get github.com/spf13/cobra@commit_hash

Use replace directives to mandatorily reference a particular module

Sometimes, you may want to use a library but a slightly modified version. It happens very often when we develop the library but test it in the context of an application. Go has a handy mechanism in go modules that can help us with it.

To make it work, you have to clone the library to your repository and modify go.mod as the following:

module github.com/myorg/app

require (
	github.com/thirdpart/library v1.1.0
)
replace (
        github.com/thirdpart/library => github.com/myorg/my-forked

)

From this moment, every time you compile the application, the updated dependency will be used.

Remove an external module

  1. Remove all code referring to that external module, including variables, functions, and module import
  2. Run the command go mod tidy to remove those unused dependencies from go.mod
  3. Run the command go mod vendor to regenerate the vendor directory
  4. Submit the code change to GitHub