Golang is a simple and efficient development language, but package management has always been a pain point:
- Many open source projects, especially github.com in the large number of applications golang.org and google.golang.org in the source code, and because the wall causes will also lead to a variety of not smooth, copy Gopath content in the team more complex when more
- Package before the relationship is complex, it is not possible to know exactly what project sources are referenced in the package, or even some of the problems caused by the mistaken deletion
- The inability to accurately manage versions of the source package can cause some project compilation to fail
Unlike in Java, there are maven or jar, donet dll files and so on, so that users more convenient.
Go has introduced the Vendor property in version 1.5 (the default is off, you need to set the GO environment variable go15vendorexperiment=1), and the vendor property is turned on by default in version 1.6.
Vendor is the go compile, priority from the project source root directory Vendor directory to find the code (can be understood as a cut gopath), if there is vendor, then no longer go to Gopath to find.
Of course Golang Package management also has several kinds of programs (GODEP/GOVENDOR/GVT, etc.), the pattern is basically similar, compared to glide more convenient, so focus on the use of glide
-
Glide installation
go get github.com/Masterminds/glide
After the installation is complete, there will be a Glide.exe file in the%gopath%/bin directory
- Configure glide environment variables to facilitate later use of glide directives
-
Switch to the project root to initialize the Yaml file with the Glide init instruction
glide init
The complete directory after initialization is as follows
Glide.yaml content, because Main.go only temporarily only fmt.println ("Hello World")
package: github.com/nickchou/glide-goimport: []
-
Lead a package test
glide get github.com/mattn/go-adodb
If the following error is reported ():
Unable to export dependencies to vendor Directory:error moving files:exit status 1. Output
Workaround: Locate the%gopath%/src/github.com/masterminds/glide/path/winbug.go file and modify about 75 lines
func CustomRename(o, n string) error {// Handking windows cases firstif runtime.GOOS == "windows" { msg.Debug("Detected Windows. Moving files using windows command") //cmd := exec.Command("cmd.exe", "/c", "move", o, n) cmd := exec.Command("cmd.exe", "/c", "xcopy /s/y", o, n+"\\") //新增这一行代码 output, err := cmd.CombinedOutput() if err != nil { return fmt.Errorf("Error moving files: %s. output: %s", err, output) } return nil} else if detectWsl() { cmd := exec.Command("mv", o, n) output, err2 := cmd.CombinedOutput() msg.Debug("Detected Windows Subsystem for Linux. Removing files using subsystem command") if err2 != nil { return fmt.Errorf("Error moving files: %s. output: %s", err2, output) } return nil}return os.Rename(o, n)}
After recompiling, copy the Glide.exe file to the%gopath%/bin directory "important"
-
Re-import the package, if the main function is used in the package, and the package contains other project sources, will also download attention to the wall, especially the project source on the golang.org
glide get github.com/mattn/go-adodb
Import a single package source
glide get --all-dependencies -s -v github.com/mattn/go-adodb
The directory structure for importing a single source source is as follows
-
Imports items based on a specified version number, such as
glide get github.com/go-sql-driver/mysql#v1.2
Glide.yaml file contents are as follows
-
After installation, the actual use of import will be preferred in the vendor directory to find code source
Golang Package Management Glide Instructions for use under window