当前位置:网站首页>Go path customized project path package dependency

Go path customized project path package dependency

2022-06-24 16:37:00 Constant constant

go Our projects are basically go module Pattern , Fast and convenient package dependency and version management , But sometimes some old projects need maintenance , Is still go path Pattern , This old model may be chosen by some new scholars with a little knowledge go module, Old scholars may have long been forgotten , Record from this .

One 、 Introduction to global environment path

GOROOT=C:/go

1、goroot/src Next is go Own package .

2、go mod In mode ,gopath/pkg/mod/ If you can't find a package, it will be sent from root/src Path finding

3、go path In mode ,gopath/src/ Packages that don't exist in them will also come from root/src Path finding

GOPATH=E:/go

1、 No matter what go mod still gopath Pattern , This directory needs to have .

2、 if go mod Pattern , The third-party dependent packages will be in this directory ./pkg/mod Next , Here's the picture :

3、 if go path Pattern , The third-party dependent packages will be in this directory ./src Next , Here's the picture :

Two 、go path Project deployment under mode

go mod Your code can be placed anywhere , This is relative gopath A convenient place , No matter where the project code is , The dependent packages are all as mentioned above , All in gopath/pkg/mod Next , Is the absolute path containing .

but go path The general expression of ,gopath Project code , Need to put in gopath/src Under the table of contents , It's right gopth/src Relative inclusion of , Here comes a question , If I have two projects , There are lib My bag , If the code is lost in gopath/src Under the table of contents , The code is mixed up ?

Obviously there are solutions , as follows , I want to put the project in any directory E:/marketwork Next , Terminal execution git clone xxxx src, take git Code clone Down to the directory src Under the table of contents (src Don't create... Manually )

1、 Solve the package dependency problem of the project itself :

After that, you will find that there is a problem with the local package , Because all from the overall situation gopath/src To include , And your code is not gopath/src Next .

How to solve ? With golang For example , Set up the go path that will do , Here's the picture :

2、 solve github Package dependency problem :

After setting up , Local inclusion problem resolved , The project itself contains already green , Reopen ide Of terminal,run Find a third party github There is a problem with the dependent package of

This is the time , If you execute directly under this terminal go get github.com/xxxxxxx, You will find that the package is downloaded in the current directory , What's the problem ? There is no problem with this project , Because the picture above shows , Set the project's gopath, It is in the overall situation gopath and goroot Missing package , Finally, it will come to the project gopath Go find this bag .

But this will cause duplication and redundancy of package files , because github Bag on , Many projects could have been shared , There is no need to make a copy of each project's own directory . And down to the overall situation gopath/src Next , In the future, other projects will use , You can directly find , No more downloads , How to github From the package on to the global gopath/src Well ?

It's very simple , Open another terminal , The current terminal's gopath The environment change is set to global only , Again go get xxxx, It is downloaded to the global gopath/src Next , So in gopath In mode ,go get Where is the downloaded package , Depends on the current terminal settings go environment variable GOPATH What's your directory .

Pictured , The downloaded package is in the global gopath Next
原网站

版权声明
本文为[Constant constant]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/04/20210414213842276k.html