go語言,第三方包相對路徑導入包引發的問題及解決方案(goquery)

對go語言而言,跟蹤init很顯然包有且僅有一次被導入的可能。linux

可是重複引用了goquery包,後編譯出現問題 git

項目涉及相關目錄github

├── main.go
└── parse
    └── parse.go

golang

parse包和main.go都導入了 goquery包ide

main.go 經過 import("./parse")導入parse包oop

go run 和 go build 都提示錯誤ui

# command-line-arguments
/usr/lib/golang/pkg/tool/linux_amd64/link: cannot open file /usr/lib/golang/pkg/linux_amd64/github.com/PuerkitoBio/goquery.a: open /usr/lib/golang/pkg/linux_amd64/github.com/PuerkitoBio/goquery.a: no such file or directoryspa

main.go 在不導入 parse時並沒有錯誤,說明確定是parse的鍋code

查看了gopath pkg 裏明明有.a文件 可是爲何尋找到去goroot下尋找.a文件blog

最後先是找到了解決方案 把parse導入方式切換到絕對路徑(這裏的絕對路徑是相對於gopath或者goroot而言,即包的真實路徑去掉gopath/src或者goroot/src)導入便可,後發現編譯狀況下link參數是空目錄

go run -n  main.go 命令僅打印其執行過程當中用到的全部命令,而不去真正執行它們。用這個命令跟蹤編譯過程是十分合適的。

運行結果以下

 1 cd /home/fdf/golang/src/distributed_spider/parse
 2 /usr/lib/golang/pkg/tool/linux_amd64/compile -o $WORK/_/home/fdf/golang/src/distributed_spider/parse.a -trimpath $WORK -goversion go1.9.4 -p _/home/fdf/golang/src/distributed_spider/parse -complete -buildid 310d9ff51b345d53a619be2244f0ac16bed09600 -D _/home/fdf/golang/src/distributed_spider/parse -I $WORK -I /home/fdf/golang/pkg/linux_amd64 -pack ./parse.go
 3 
 4 #
 5 # command-line-arguments
 6 #
 7 
 8 mkdir -p $WORK/command-line-arguments/_obj/
 9 mkdir -p $WORK/command-line-arguments/_obj/exe/
10 cd /home/fdf/golang/src/distributed_spider
11 /usr/lib/golang/pkg/tool/linux_amd64/compile -o $WORK/command-line-arguments.a -trimpath $WORK -goversion go1.9.4 -p main -complete -buildid 69bf6dce457753ca5df015e4ac3556bd5ca464b1 -dwarf=false -D _/home/fdf/golang/src/distributed_spider -I $WORK -I /home/fdf/golang/pkg/linux_amd64 -pack ./main.go
12 cd .
13 /usr/lib/golang/pkg/tool/linux_amd64/link -o $WORK/command-line-arguments/_obj/exe/main -L $WORK -L "" -s -w -extld=gcc -buildmode=exe -buildid=69bf6dce457753ca5df015e4ac3556bd5ca464b1 $WORK/command-line-arguments.a
14 $WORK/command-line-arguments/_obj/exe/main
相對路徑導入狀況
 1 #
 2 # distributed_spider/parse
 3 #
 4 
 5 mkdir -p $WORK/distributed_spider/parse/_obj/
 6 mkdir -p $WORK/distributed_spider/
 7 cd /home/fdf/golang/src/distributed_spider/parse
 8 /usr/lib/golang/pkg/tool/linux_amd64/compile -o $WORK/distributed_spider/parse.a -trimpath $WORK -goversion go1.9.4 -p distributed_spider/parse -complete -buildid 310d9ff51b345d53a619be2244f0ac16bed09600 -D _/home/fdf/golang/src/distributed_spider/parse -I $WORK -I /home/fdf/golang/pkg/linux_amd64 -pack ./parse.go
 9 
10 #
11 # command-line-arguments
12 #
13 
14 mkdir -p $WORK/command-line-arguments/_obj/
15 mkdir -p $WORK/command-line-arguments/_obj/exe/
16 cd /home/fdf/golang/src/distributed_spider
17 /usr/lib/golang/pkg/tool/linux_amd64/compile -o $WORK/command-line-arguments.a -trimpath $WORK -goversion go1.9.4 -p main -complete -buildid fbcca07f20e1f56c4f1a586a3c879bde6e112ba5 -dwarf=false -D _/home/fdf/golang/src/distributed_spider -I $WORK -I /home/fdf/golang/pkg/linux_amd64 -pack ./main.go
18 cd .
19 /usr/lib/golang/pkg/tool/linux_amd64/link -o $WORK/command-line-arguments/_obj/exe/main -L $WORK -L /home/fdf/golang/pkg/linux_amd64 -s -w -extld=gcc -buildmode=exe -buildid=fbcca07f20e1f56c4f1a586a3c879bde6e112ba5 $WORK/command-line-arguments.a
20 $WORK/command-line-arguments/_obj/exe/main
絕對路徑導入狀況

跟蹤發現 有差別的狀況的確是 在相對路徑下 -L的參數缺失了 goopath(相對..狀況13行和絕對..狀況19行)

致使 link去goroot下尋找 

至於爲何會出現這種狀況  還沒弄明白,感受真的像編譯這邊的bug。。。 明明前面編譯的過程當中都是能尋找到並輸入.a文件的

可是導入包的時候儘可能寫絕對路徑。

相關文章
相關標籤/搜索