Golang官方依赖管理工具:dep
在这里声明一下,百度或者google看到的
下面进入教程。 介绍dep是一个原型依赖管理工具,需要在Go 1.7及更高的版本中使用,说明第三方工具近期还是有市场的。 安装环境准备。//设置环境变量使用vendor目录GO15VENDOREXPERIMENT=1 安装dep等到 $goget-ugithub.com/golang/dep/cmd/dep 验证安装$dep depisatoolformanagingdependenciesforGoprojectsUsage:dep<command>Commands:initInitializeanewprojectwithmanifestandlockfiles statusReportthestatusoftheproject'sdependencies ensureEnsureadependencyissafelyvendoredintheproject prunePrunethevendortreeofunusedpackagesExamples: depinitsetupanewproject depensureinstalltheproject'sdependencies depensure-updateupdatethelockedversionsofalldependencies depensure-addgithub.com/pkg/errorsaddadependencytotheprojectUse"dephelp[command]"formoreinformationaboutacommand. 有一个很重要的选项 看到这个说明已经安装好了。 使用老规矩,篇幅有限,我只介绍经常使用到的。 先进入在GOPATH的一个项目中。 cd$GOPATH/src/foordep 初始化(dep init)$cdfoordep/ $depinit $ll total12 -rw-rw-r--1qiangmzsxqiangmzsx286Aug711:45Gopkg.lock-rw-rw-r--1qiangmzsxqiangmzsx535Aug711:45Gopkg.tomldrwxrwxr-x2qiangmzsxqiangmzsx4096Aug711:45vendor 大家发现了,应用foordep目录下出现了两个文件(Gopkg.lock、Gopkg.toml)和一个目录(vendor)。 它们是什么关系呢? 所以出现 Gopkg.toml and Gopkg.lock are out of sync. 时候最好执行一下dep ensure 。
下面看看它们的内容。 $catGopkg.lock #Thisfileisautogenerated,donotedit;changesmaybeundonebythenext'depensure'.[solve-meta] analyzer-name="dep" analyzer-version=1 inputs-digest="ab4fef131ee828e96ba67d31a7d690bd5f2f42040c6766b1b12fe856f87e0ff7" solver-name="gps-cdcl" solver-version=1$catGopkg.toml #Gopkg.tomlexample##Refertohttps://github.com/golang/dep/blob/master/docs/Gopkg.toml.md#fordetailedGopkg.tomldocumentation.##required=["github.com/user/thing/cmd/thing"]#ignored=["github.com/user/project/pkgX","bitbucket.org/user/project/pkgA/pkgY"]##[[constraint]]#name="github.com/user/project"#version="1.0.0"##[[constraint]]#name="github.com/user/project2"#branch="dev"#source="github.com/myfork/project2"##[[override]]#name="github.com/x/y"#version="2.4.0" dep ensure我们写一个 #必需包required=["github.com/astaxie/beego"]#忽略包ignored=["golang.org/x/crypto"]#项目元数据[metadata] homepage="https://github.com/qiangmzsx"license="MIT"owners_name_1="qiangmzsx"owners_email_1="qiangmzsx@hotmail.com"owners_homepage_1="https://github.com/qiangmzsx"#约束条件[[constraint]] name="github.com/astaxie/beego" #可选:版本 version="=1.8.0" #分支 #branch="master" #修订 #revision="beego1.8.0" #可选:指定来源 source="github.com/astaxie/beego" 但是怎么样执行?可以执行如下命令寻找帮助: $dephelpensureUsage:depensure[-update|-add][-no-vendor|-vendor-only][-dry-run][<spec>...]Projectspec: <importpath>[:altsourceURL][@<constraint>] Ensuregetsaprojectintoacomplete,reproducible,andlikelycompilablestate: *Allnon-stdlibimportsarefulfilled *AllrulesinGopkg.tomlarerespected *Gopkg.lockrecordspreciseversionsforalldependencies *vendor/ispopulatedaccordingtoGopkg.lock Ensurehasfasttechniquestodeterminethatsomeofthesestepsmaybe unnecessary.Ifthatdeterminationismade,ensuremayskipsomesteps.Flags maybepassedtobypassthesechecks;-vendor-onlywillallowanout-of-date Gopkg.locktopopulatevendor/,and-no-vendorwillupdateGopkg.lock(ifneeded),butnevertouchvendor/. Theeffectofpassingprojectspecargumentsvariesslightlydependingonthe combinationofflagsthatarepassed. Examples: depensurePopulatevendorfromexistingGopkg.tomlandGopkg.lock depensure-addgithub.com/pkg/fooIntroduceanameddependencyatitsnewestversion depensure-addgithub.com/pkg/foo@^1.0.1IntroduceanameddependencywithaparticularconstraintFormoredetailedusageexamples,seedepensure-examples. Flags: -addaddnewdependencies,orpopulateGopkg.tomlwithconstraintsforexistingdependencies(default:false) -dry-runonlyreportthechangesthatwouldbemade(default:false) -examplesprintdetailedusageexamples(default:false) -no-vendorupdateGopkg.lock(ifneeded),butdonotupdatevendor/(default:false) -updateupdatethenameddependencies(orall,ifnonearenamed)inGopkg.locktothelatestallowedbyGopkg.toml(default:false) -venableverboselogging(default:false) -vendor-onlypopulatevendor/fromGopkg.lockwithoutupdatingitfirst(default:false) 执行一下 $depensurealldirslackedanygocode 错误了,这是因为foordep目录下没有任何的go代码,只能加上一个看看。 $vimmain.gopackagemainimport("github.com/astaxie/beego" "runtime")funcmain(){ maxCPU:=runtime.NumCPU() runtime.GOMAXPROCS(maxCPU) beego.Run() } 再来试试看。 $depensure$llvendor/ total4drwxrwxr-x3qiangmzsxqiangmzsx4096Aug716:29github.com 看看Gopkg.lock的内容。 #Thisfileisautogenerated,donotedit;changesmaybeundonebythenext'depensure'.[[projects]] name="github.com/astaxie/beego" packages=[".","config","context","grace","logs","session","toolbox","utils"] revision="323a1c4214101331a4b71922c23d19b7409ac71f" source="github.com/astaxie/beego" version="v1.8.0"[solve-meta] analyzer-name="dep" analyzer-version=1 inputs-digest="6fb93334da1b165aaab11f170d871a92b40033575e66e2cf4289e77e0d64551d" solver-name="gps-cdcl" solver-version=1 现在需要解析json,我们试试使用命令行的方式导入 $depensure-addgithub.com/bitly/go-simplejson $Gopkg.lock #Thisfileisautogenerated,"utils"] revision="323a1c4214101331a4b71922c23d19b7409ac71f" source="github.com/astaxie/beego" version="v1.8.0"[[projects]] name="github.com/bitly/go-simplejson" packages=["."] revision="aabad6e819789e569bd6aabf444c935aa9ba1e44" version="v0.5.0"[solve-meta] analyzer-name="dep" analyzer-version=1 inputs-digest="6fb93334da1b165aaab11f170d871a92b40033575e66e2cf4289e77e0d64551d" solver-name="gps-cdcl" solver-version=1 可以发现多了 $depensure-addgithub.com/bitly/go-simplejson Gopkg.tomlandGopkg.lockareoutofsync.Runaplaindepensuretoresyncthembeforeattemptingto-add 还可以指定依赖的版本: $depensure-addgithub.com/bitly/go-simplejson@=0.4.3 是因为 #必需包 required=["github.com/astaxie/beego"] #忽略包 ignored=["golang.org/x/crypto"] #项目元数据 [metadata] homepage="https://github.com/qiangmzsx"license="MIT"owners_name_1="qiangmzsx"owners_email_1="qiangmzsx@hotmail.com"owners_homepage_1="https://github.com/qiangmzsx"#约束条件[[constraint]] name="github.com/astaxie/beego" #可选:版本 version="=1.8.0" #分支 #branch="master" #修订 #revision="beego1.8.0" #可选:指定来源 source="github.com/astaxie/beego"[[constraint]] name="github.com/bitly/go-simplejson" branch="master" source="https://github.com/bitly/go-simplejson.git"
^1.2.3意味1.2.3<=X<2.0.0^0.2.3意味0.2.3<=X<0.3.0^0.0.3意味0.0.3<=X<0.1.0 如果执行 $depensure errorwhileparsing/home/users/qiangmzsx/golang/src/foordep/Gopkg.toml:multipleconstraintsspecifiedforgithub.com/astaxie/beego,canonlyspecifyone 说明配置写错了,需要看看 空配置我们现在尝试着把 packagemainimport("github.com/astaxie/beego" "github.com/bitly/go-simplejson" "runtime")funcmain(){maxCPU:=runtime.NumCPU() runtime.GOMAXPROCS(maxCPU)strJson:=`{"announcer":{"nickname":"非议讲史","kind":"user","created_at":1494904539000,"updated_at":1494983507000,"track_id":38088960}}` mapJson,_:=simplejson.NewJson([]byte(strJson))println(mapJson) beego.Run() } 执行 $depinit-v Rootprojectis"foordep" 1transitivelyvalidinternalpackages 2externalpackagesimportedfrom2projects (0)select(root) (1)?attemptgithub.com/bitly/go-simplejsonwith1pkgs;5versionstotry (1)trygithub.com/bitly/go-simplejson@v0.5.0 (1)selectgithub.com/bitly/go-simplejson@v0.5.0w/1pkgs (2)?attemptgithub.com/astaxie/beegowith1pkgs;23versionstotry (2)trygithub.com/astaxie/beego@v1.8.3 (2)selectgithub.com/astaxie/beego@v1.8.3w/9pkgs foundsolutionwith10packagesfrom2projects Solverwalltimesbysegment: b-list-versions:3.926086724s b-list-pkgs:285.209471ms b-gmal:266.828805msselect-atom:3.417834ms satisfy:3.126864msselect-root:428.276μsnew-atom:234.106μs other:45.014μs b-source-exists:6.946μs b-deduce-proj-root:3.472μs TOTAL:4.485387512sUsing^0.5.0asconstraintfordirectdepgithub.com/bitly/go-simplejson Lockinginv0.5.0(aabad6e)fordirectdepgithub.com/bitly/go-simplejsonUsing^1.8.3asconstraintfordirectdepgithub.com/astaxie/beego Lockinginv1.8.3(cab8458)fordirectdepgithub.com/astaxie/beego 此时再查看 $vimGopkg.toml[[constraint]] name="github.com/astaxie/beego" version="1.8.3"[[constraint]] name="github.com/bitly/go-simplejson" version="0.5.0"$vimGopkg.lock[[projects]] name="github.com/astaxie/beego" packages=[".","context/param","utils"] revision="cab8458c1c4a5a3b4bf5192922be620e6dede15b" version="v1.8.3"[[projects]] name="github.com/bitly/go-simplejson" packages=["."] revision="aabad6e819789e569bd6aabf444c935aa9ba1e44" version="v0.5.0"[solve-meta] analyzer-name="dep" analyzer-version=1 inputs-digest="dc040fb12390d61768be6388ad2935bdd7f9cc93d4b1fdda421a284058277c80" solver-name="gps-cdcl" solver-version=1 与 dep cache看到这里时候很多人都会有疑问? $GOPATH/src不存在依赖包环境准备,将原来的cache和vendor清空,别遗漏了 $ll total4-rwxr--r--1qiangmzsxqiangmzsx990Aug716:39main.go $vimmain.go packagemainimport("github.com/astaxie/beego" "github.com/bitly/go-simplejson" "runtime") funcmain(){maxCPU:=runtime.NumCPU() runtime.GOMAXPROCS(maxCPU)strJson:=`{"announcer":{"nickname":"非议讲史",_:=simplejson.NewJson([]byte(strJson)) println(mapJson) beego.Run() } 执行 $ll total4 -rwxr--r--1qiangmzsxqiangmzsx382Aug812:47main.go$depinit-gopath-v SearchingGOPATHforprojects... FollowingdependencieswerenotfoundinGOPATH.Depwillusethemostrecentversionsoftheseprojects. github.com/bitly/go-simplejson Rootprojectis"foordep" 1transitivelyvalidinternalpackages2externalpackagesimportedfrom2projects (0)select(root) (1)?attemptgithub.com/astaxie/beegowith1pkgs;atleast1versionstotry (1)trygithub.com/astaxie/beego@76ce912a30f9255d8d353d365ab99cb069fd29e0 (1)Unabletoupdatecheckedoutversion:fatal:referenceisnotatree:76ce912a30f9255d8d353d365ab99cb069fd29e0 (1)trygithub.com/astaxie/beego@v1.8.3(1)selectgithub.com/astaxie/beego@v1.8.3w/9pkgs (2)?attemptgithub.com/bitly/go-simplejsonwith1pkgs;5versionstotry (2)trygithub.com/bitly/go-simplejson@v0.5.0 (2)selectgithub.com/bitly/go-simplejson@v0.5.0w/1pkgs foundsolutionwith10packagesfrom2projects Solverwalltimesbysegment: b-list-pkgs:320.955115ms b-gmal:274.950203ms satisfy:8.179966msselect-atom:2.62224msnew-atom:392.168μs b-list-versions:254.937μsselect-root:209.152μs b-deduce-proj-root:40.45μs other:37.01μs b-source-exists:5.83μs TOTAL:607.647071msUsing^1.8.3asconstraintfordirectdepgithub.com/astaxie/beego Lockinginv1.8.3(cab8458)fordirectdepgithub.com/astaxie/beegoUsing^0.5.0asconstraintfordirectdepgithub.com/bitly/go-simplejson Lockinginv0.5.0(aabad6e)fordirectdepgithub.com/bitly/go-simplejson 日志显示, $GOPATH存在依赖包环境准备,将原来的cache和vendor清空,注意 $ll total4 -rwxr--r--1qiangmzsxqiangmzsx382Aug812:47main.go$depinit-gopath-v SearchingGOPATHforprojects... Usingmasterasconstraintfordirectdepgithub.com/bitly/go-simplejson Lockinginmaster(da1a892)fordirectdepgithub.com/bitly/go-simplejson Rootprojectis"foordep" 1transitivelyvalidinternalpackages 2externalpackagesimportedfrom2projects (0)select(root) (1)?attemptgithub.com/astaxie/beegowith1pkgs;atleast1versionstotry (1)trygithub.com/astaxie/beego@76ce912a30f9255d8d353d365ab99cb069fd29e0 (1)Unabletoupdatecheckedoutversion:fatal:referenceisnotatree:76ce912a30f9255d8d353d365ab99cb069fd29e0 (1)trygithub.com/astaxie/beego@v1.8.3(1)selectgithub.com/astaxie/beego@v1.8.3w/9pkgs (2)?attemptgithub.com/bitly/go-simplejsonwith1pkgs;atleast1versionstotry (2)trygithub.com/bitly/go-simplejson@master (2)selectgithub.com/bitly/go-simplejson@masterw/1pkgs foundsolutionwith10packagesfrom2projects Solverwalltimesbysegment: b-list-pkgs:312.646734ms b-gmal:265.066047ms satisfy:6.488056msselect-atom:3.287416msnew-atom:397.837μsselect-root:373.267μs b-list-versions:108.466μs other:47.43μs b-source-exists:7.71μs b-deduce-proj-root:6.568μs TOTAL:588.429531msUsing^1.8.3asconstraintfordirectdepgithub.com/astaxie/beego Lockinginv1.8.3(cab8458)fordirectdepgithub.com/astaxie/beego 可以看到
在 更新配置 (dep ensure -update)现在修改 $vimGopkg.toml [[constraint]] name="github.com/astaxie/beego" #约束为1.8.0 version="=1.8.0"[[constraint]] name="github.com/bitly/go-simplejson" version="0.5.0"$depensure-update Gopkg.tomlandGopkg.lockareoutofsync.Runaplaindepensuretoresyncthembeforeattemptingto-update$depensure$depensure-update-v Rootprojectis"foordep" 1transitivelyvalidinternalpackages2externalpackagesimportedfrom2projects (0)select(root) (1)?attemptgithub.com/astaxie/beegowith1pkgs;23versionstotry (1)trygithub.com/astaxie/beego@v1.8.3(2)github.com/astaxie/beego@v1.8.3notallowedbyconstraint1.8.0: (2)1.8.0from(root) (1)trygithub.com/astaxie/beego@v1.8.2(2)github.com/astaxie/beego@v1.8.2notallowedbyconstraint1.8.0: (2)1.8.0from(root) (1)trygithub.com/astaxie/beego@v1.8.1(2)github.com/astaxie/beego@v1.8.1notallowedbyconstraint1.8.0: (2)1.8.0from(root) (1)trygithub.com/astaxie/beego@v1.8.0(1)selectgithub.com/astaxie/beego@v1.8.0w/8pkgs (2)?attemptgithub.com/bitly/go-simplejsonwith1pkgs;5versionstotry (2)trygithub.com/bitly/go-simplejson@v0.5.0(2)selectgithub.com/bitly/go-simplejson@v0.5.0w/1pkgs foundsolutionwith9packagesfrom2projects Solverwalltimesbysegment: b-source-exists:4.00794324s b-list-pkgs:2.545452669s b-gmal:276.070372mssatisfy:5.179016ms select-atom:4.337704ms new-atom:1.359055ms b-list-versions:467.799μs b-matches:371.239μs select-root:193.471μs b-pair-rev:127.992μsother:25.962μs b-pair-version:7.866μsTOTAL:6.841536385s$depstatus PROJECTCONSTRAINTVERSIONREVISIONLATESTPKGSUSED github.com/astaxie/beego1.8.0v1.8.0323a1c4323a1c48github.com/bitly/go-simplejson^0.5.0v0.5.0aabad6eaabad6e1 后记看到了这里,那么对 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |