英文:
use of import statement
问题
我最近几周一直在玩Go,目前为止还不错。
现在我正在编写一个跨不同文件的程序,像这样分割:
.
|-- geometry
| |-- cone
| `-- cone.go
|-- main.go
|-- Makefile
问题是我无法在main.go中导入cone.go,编译器找不到它。
有人知道怎么解决吗?
英文:
I've been toying around Go for a couple of weeks now, so far so good.
Now I am writing a program splitted across different files like this:
.
|-- geometry
| |-- cone
| `-- cone.go
|-- main.go
|-- Makefile
the problem is i can't import cone.go in the main.go, the compiler doesn't find it.
Anybody?
答案1
得分: 2
如果你不介意阅读一些内容,这个链接有一个关于你所问问题的长篇讨论。
以下是一个简短的回答。
Import在$GOROOT/pkg中查找包(如果我没记错的话),它不会在本地目录中查找。你可以为"geometry"创建一个单独的makefile,使用go包的makefile包含(参见这里),然后让你的主makefile创建该包,并传递<code>-I</code>来包含./geometry中的新包。
英文:
If you don't mind a bit of reading, this link has a lengthy discussion on the problem you're asking about.
Here's a short answer.
Import looks for package in $GOROOT/pkg (IIRC), it does not look in local directories. What you can do is make a seperate makefile for "geometry" using the go package makefile includes (see here) and then have your main makefile make the package and pass the <code>-I</code> to include the new package in ./geometry
答案2
得分: 0
标志:
-o file
输出文件,默认为6.out用于6g等。
-e 通常编译器在10个错误后退出;-e打印所有错误
-I dir1 -I dir2
将dir1和dir2添加到检查导入包的路径列表中
-N 禁用优化
-S 将汇编语言文本写入标准输出
-V 打印编译器版本
尝试将-I geometry
添加到您的编译器选项中。
英文:
From the gc docs:
Flags:
-o file
output file, default 6.out for 6g, etc.
-e normally the compiler quits after 10 errors; -e prints all errors
-I dir1 -I dir2
add dir1 and dir2 to the list of paths to check for imported packages
-N disable optimization
-S write assembly language text to standard output
-V print the compiler version
Try adding -I geometry
to your compiler options.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论