Home > Article > Backend Development > A brief analysis of how to install and use dep in golang
Golang is a strongly typed programming language developed by Google. In the eyes of programmers, Golang's C-like language style and rich standard library make it an excellent development language. In order to develop a Golang project, it is often necessary to use a Golang-compatible dependency management tool. Among them, dep is a currently popular dependency management tool. Now we officially start to learn the settings of dep.
1. Install dep
dep is a dependency management tool that creates a vendor folder in the GOPATH/src directory and places dependent packages in the folder. Otherwise, dependencies will be saved directly in the GOPATH/src directory, which will cause dependency conflicts.
First we need to install dep. It is recommended to use go get to install dep in the golang environment. Execute the following command to complete the installation of dep.
go get -u github.com/golang/dep/cmd/dep
After the installation is completed, we need to check the installation status of dep. We can run dep in the command line, and relevant information will be displayed, indicating that dep has been successfully installed.
2. Initialize dep
Now you need to initialize a project in order to use dep as a dependency management tool. It is assumed that the creation of a golang project and source code version control have been completed, and the project folder has been pushed to the warehouse. Now, we can open the terminal and go to the project directory and run the following command.
dep init
This initializes the dep file and saves the generated file in the project folder. dep will automatically scan the source code of the project and generate a Gopkg.toml file, which records the metadata information of the project and all its dependent packages, and also generates a vendor directory, which is used to store the source code of the dependent packages (dependency The Git URL of the item, or similar source code, contained in the vendor, not under $GOPATH/src/).
3. Add dependencies
The most useful function of dep to manage Golang project dependencies is to track dependencies and automatically install dependencies. We can add dependencies using the following command in the root directory of the project.
dep ensure -add github.com/example/mypackage
This will add new package and version constraints to the Gopkg.toml file and save them in a Gopkg.lock file called the lock file. The lock file lists all dependencies and their version numbers. Therefore, the file must be in the same code base as source control.
4. Update dependencies
If you need to update the dependencies in the project, you can use the following command:
dep ensure -update
This will upgrade all dependencies in the project and upgrade each dependency to its The latest version of.
5. Delete dependencies
If you no longer need to use dependencies in the project, you can use the following command to remove dependencies from Gopkg.toml.
dep ensure -remove github.com/example/badpackage
This will remove the specified dependencies from the Gopkg.toml file.
Summary
dep is a powerful dependency management tool that makes using dependencies in Golang projects easier. Programmers can easily manage and maintain all dependencies in a project by initializing, adding, updating, and removing dependencies. In addition to dep, there are some other dependency management tools to choose from, such as gvt, glide, etc. However, dep is relatively commonly used among existing dependency management tools because it is an officially recommended tool.
The above is the detailed content of A brief analysis of how to install and use dep in golang. For more information, please follow other related articles on the PHP Chinese website!