对go语言而言,跟踪init很显然包有且仅有一次被导入的可能。
但是重复引用了goquery包,后编译出现问题
项目涉及相关目录
├── main.go
└── parse
└── parse.go
parse包和main.go都导入了 goquery包
main.go 通过 import("./parse")导入parse包
go run 和 go build 都提示错误
# 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 directory
main.go 在不导入 parse时并无错误,说明肯定是parse的锅
查看了gopath pkg 里明明有.a文件 但是为什么寻找到去goroot下寻找.a文件
最后先是找到了解决方案 把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文件的
但是导入包的时候尽量写绝对路径。
原文地址:https://www.cnblogs.com/Geek-xiyang/p/10232364.html