我刚刚组装了一个Go包,它将成为一个相当大的系统的一部分,其中有很多共享的包。我能够通过编写它的Makefile来编译它,这样编译器就可以用-I标志来调用:
include $(GOROOT)/src/Make.inc
TARG=foobar
GOFILES=\
foobar.go\
foobar:
$(GC) -I$(CURDIR)/../intmath -I$(CURDIR)/../randnum foobar.go
include $(GOROOT)/src/Make.pkg它编译得很好,作为一个好孩子,我写了一套全面的测试。但是,当我尝试使用gotest运行测试时,我得到了一个编译错误:
$ gotest
rm -f _test/foobar.a
8g -o _gotest_.8 foobar.go foobar_test.go
foobar.go:4: can't find import: intmath
make: *** [_gotest_.8] Error 1
gotest: "C:\\msys\\bin\\sh.exe -c \"gomake\" \"testpackage\" \"GOTESTFILES=foobar_test.go\"" failed: exit status 2因此,当我使用-I标志告诉Go文件在哪里可以找到intmath和randnum包时,Go文件本身就会被编译,但是gotest似乎并没有使用Makefile。
回答peterSO的问题:foobar.go的import部分如下所示:
import (
"intmath"
"randnum"
"container/vector"
)只要我将-I标志发送到编译器,编译就能正常工作。我尝试使用相对路径,如下所示:
import (
"../intmath"
"../randnum"
"container/vector"
)但这似乎并不管用。
编辑:回答更多peterSO问题:
GOROOT被设置为C:\Go,这个目录是我安装所有Go程序的地方--除了我的源代码。我期望相对路径相对于源文件所在的目录。
我的源码树看起来像这样:
server/
foobar/
randnum/
intmath/因此,虽然我对一种不同的、更通用的目录结构持开放态度,但我的直觉是将它们安排为同级。
有没有什么方法可以让gotest使用所需的标志来编译foobar.go?
发布于 2011-07-27 06:00:38
创建Windows源代码目录结构:
C:\server
C:\server\foobar
C:\server\intnum对于intnum.go:
package intnum
func IntNum() int {
return 42
}Makefile:
include $(GOROOT)/src/Make.inc
TARG=server/intnum
GOFILES=\
intnum.go\
include $(GOROOT)/src/Make.pkg运行:
$ cd c/server/intnum
$ make install对于foobar.go:
package foobar
import (
"math"
"server/intnum"
)
func FooBar() float64 {
return float64(intnum.IntNum()) * math.Pi
}Makefile:
include $(GOROOT)/src/Make.inc
TARG=server/foobar
GOFILES=\
foobar.go\
include $(GOROOT)/src/Make.pkg运行:
$ cd /c/server/foobar
$ make install安装完成后,intnum.a和foobar.a包文件将位于$GOROOT\pkg\windows_386\server (C:\Go\pkg\windows_386\server)目录`中。
https://stackoverflow.com/questions/6836006
复制相似问题