英文:
Building package structure with child-/sub-packages
问题
我正在尝试用Go语言制作一个简单的计算器。我设计它的方式是先构建一个命令行界面,然后可以轻松地切换到GUI界面。项目位置是$GOPATH/src/gocalc(此后所有路径都是相对于项目位置的)。命令行界面的逻辑存储在一个名为gocalc.go的文件中。计算器逻辑存储在calcfns/calcfns.go和operations/operations.go两个文件中。所有文件的包名与其文件名相同(不包括扩展名),除了主程序gocalc.go,它位于package main中。
calcfns.go通过import "gocalc/operations"导入operations.go;gocalc.go通过import "gocalc/calcfns"导入calcfns.go。
总结一下:
$GOPATH/src/gocalc/gocalc.gopackage mainimport "gocalc/calcfns"
calcfns/calcfns.gopackage calcfnsimport "gocalc/operations"
operations/operations.gopackage operations
当我尝试从项目目录中执行go build operations时,得到的响应是:can't load package: package operations: import "operations": cannot find package。当我尝试执行go build gocalc/operations时,得到的是can't load package: package gocalc/operations: import "gocalc/operations": cannot find package。当我尝试执行go build operations/operations.go时,它可以正常编译。
当我尝试执行go build calcfns或go build gocalc/calcfns时,得到的是类似于operations的can't load package...的消息;然而,当我尝试构建calcfns/calcfns.go时,它在导入语句上出错:import "gocalc/operations": cannot find package。
最后,当我尝试从项目目录中执行go build .时,它与前面的段落类似出错:import "gocalc/calcfns": cannot find package。
我应该如何组织我的子包和/或导入语句,以使go build不会失败?
英文:
I'm trying to make a simple calculator in Go. I'm designing it in such a way that I can build a command-line interface first and easily swap in a GUI interface. The project location is $GOPATH/src/gocalc (all paths hereafter are relative to the project location). The command-line interface logic is stored in a file gocalc.go. The calculator logic is stored in files calcfns/calcfns.go and operations/operations.go. All files have package names identical to their filename (sans extension) except the main program, gocalc.go, which is in the package main
calcfns.go imports operations.go via import "gocalc/operations"; gocalc.go imports calcfns.go via import "gocalc/calcfns"
To summarize:
$GOPATH/src/gocalc/gocalc.gopackage mainimport "gocalc/calcfns"
calcfns/calcfns.gopackage calcfnsimport "gocalc/operations"
operations/operations.gopackage operations
When I try to go build operations (from the project dir), I get the response: can't load package: package operations: import "operations": cannot find package
When I try go build gocalc/operations, I get can't load package: package gocalc/operations: import "gocalc/operations": cannot find package
When I try go build operations/operations.go, it compiles fine
When I try to go build calcfns or go build gocalc/calcfns, I get can't load package... messages, similar to those in operations; however, when I try to build calcfns/calcfns.go it chokes on the import statement: import "gocalc/operations": cannot find package
Finally, when I try go build . from the project dir, it chokes similar to the previous paragraph: import "gocalc/calcfns": cannot find package
How should I structure my child packages and/or import statements in such a way that go build won't fail?
答案1
得分: 7
愚蠢地,我忘记导出我的GOPATH变量,所以go env显示""作为GOPATH。(感谢jnml建议打印go env;+1)。
在这样做之后(并将主程序移动到自己的文件夹{project-dir}/gocalc/gocalc.go),我可以通过go install gocalc/gocalc来构建/安装程序。
故事的寓意是,确保在设置$GOPATH环境变量时键入export GOPATH=...而不仅仅是GOPATH=...。
英文:
Stupidly, I forgot to export my GOPATH variable, so go env displayed "" for GOPATH. (thanks to jnml for suggesting to print go env; +1).
After doing this (and moving the main program to its own folder {project-dir}/gocalc/gocalc.go), I could build/install the program via go install gocalc/gocalc.
Moral of the story, make sure you type export GOPATH=... instead of just GOPATH=... when setting your $GOPATH environment variable
答案2
得分: 1
请尝试添加$ go env的输出以提供更多线索。否则,目录结构和(显示的)导入语句看起来都没问题。
然而,这句话
> 当我尝试进行go build操作(从项目目录中),我得到的回应是:无法加载包:包operations:导入operations:找不到包
听起来很奇怪。这似乎暗示你在operations.go中有以下内容:
package operations
import "operations"
这可能是问题所在...
英文:
Please try to also add output of $ go env to provide more clues. Otherwise both the directories structure and (the shown) import statements looks OK.
However the sentence
> When I try to go build operations (from the project dir), I get the response: can't load package: package operations: import "operations": cannot find package
sounds strange. It seems to suggest you have
package operations
import "operations"
in 'operations.go', which would be the culprit then...?
答案3
得分: -1
-
将你的根目录放在
GOPATH/src/下,例如我的GOPATH=~/go/src(运行命令go env获取你的GOPATH)。所以我的应用的完整路径是~/go/src/golang-playground。 -
假设你想在
main.go文件中使用router.go文件中的Index()函数(当然,它们都在根目录下)。所以在main.go中:import ( ... "golang-playground/router" ) func main() { foo.Bar("/", router.Index) // 注意大写表示在文件外部是公开的 }
英文:
Very easy:
Lets say I have a project/app named: golang-playground
-
Put your root dir under
GOPATH/src/, in my caseGOPATH=~/go/src(run commandgo envto get yourGOPATH). so complete path for my app is~/go/src/golang-playground

-
Lets say you want to use function
Index()inside of file:router.gofrom mymain.gofile (which of course is on root dir). so in main.go:import ( ... "golang-playground/router" ) func main() { foo.Bar("/", router.Index) // Notice caps means its public outside of file }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论