简体   繁体   English

使用子/子包构建包结构

[英]Building package structure with child-/sub-packages

I'm trying to make a simple calculator in Go. 我想在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. 我正在设计它,我可以先构建一个命令行界面,然后在GUI界面中轻松交换。 The project location is $GOPATH/src/gocalc (all paths hereafter are relative to the project location). 项目位置是$GOPATH/src/gocalc (此后所有路径都相对于项目位置)。 The command-line interface logic is stored in a file gocalc.go . 命令行界面逻辑存储在文件gocalc.go The calculator logic is stored in files calcfns/calcfns.go and operations/operations.go . 计算器逻辑存储在calcfns/calcfns.gooperations/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 所有文件的包名都与其文件名相同(无扩展名), gocalc.go ,它位于package main

calcfns.go imports operations.go via import "gocalc/operations" ; calcfns.go进口operations.go通过import "gocalc/operations" ; gocalc.go imports calcfns.go via import "gocalc/calcfns" gocalc.go进口calcfns.go通过import "gocalc/calcfns"

To summarize: 总结一下:

  • $GOPATH/src/gocalc/
    • gocalc.go
      • package main
      • import "gocalc/calcfns"
    • calcfns/
      • calcfns.go
        • package calcfns
        • import "gocalc/operations"
    • operations/
      • operations.go
        • package 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 当我尝试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 ,编译好了

When I try to go build calcfns or go build gocalc/calcfns , I get can't load package... messages, similar to those in operations; 当我尝试go build calcfns或者go build gocalc/calcfns ,我得到的can't load package...消息,类似于操作中的消息; however, when I try to build calcfns/calcfns.go it chokes on the import statement: import "gocalc/operations": cannot find package 然而,当我尝试构建calcfns/calcfns.go它会在import语句上import "gocalc/operations": cannot find packageimport "gocalc/operations": cannot find package

Finally, when I try go build . 最后,当我尝试go build . from the project dir, it chokes similar to the previous paragraph: import "gocalc/calcfns": cannot find package 从项目目录开始,它类似于前一段: 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? 我应该如何构建我的子包和/或导入语句,使go build不会失败?

Stupidly, I forgot to export my GOPATH variable, so go env displayed "" for GOPATH . 愚蠢的是,我忘了导出我的GOPATH变量,所以对于GOPATHgo env显示"" (thanks to jnml for suggesting to print go env ; +1). (感谢jnml建议打印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 . 执行此操作(并将主程序移动到其自己的文件夹{project-dir}/gocalc/gocalc.go )后,我可以通过go install gocalc/gocalc构建/安装程序。

Moral of the story, make sure you type export GOPATH=... instead of just GOPATH=... when setting your $GOPATH environment variable 故事的道德,确保在设置$ GOPATH环境变量时键入export GOPATH=...而不是GOPATH=...

Please try to also add output of $ go env to provide more clues. 请尝试添加$ go env输出以提​​供更多线索。 Otherwise both the directories structure and (the shown ) import statements looks OK. 否则,目录结构和( 显示的 )import语句看起来都可以。

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...? 在'operations.go'中,那将是罪魁祸首......?

Very easy: 很容易:

Lets say I have a project/app named: golang-playground 假设我有一个名为golang-playground的项目/应用程序

  1. Put your root dir under GOPATH/src/ , in my case GOPATH=~/go/src (run command go env to get your GOPATH ). 把根目录放在GOPATH/src/ ,在我的例子中GOPATH=~/go/src (运行命令go env得到你的GOPATH )。 so complete path for my app is ~/go/src/golang-playground 我的应用程序的完整路径是~/go/src/golang-playground 在此输入图像描述

  2. Lets say you want to use function Index() inside of file: router.go from my main.go file (which of course is on root dir). 让我们说你想在我的main.go文件中使用文件中的函数Index()router.go (当然是在root目录下)。 so in main.go: 所以在main.go中:

     import ( ... "golang-playground/router" ) func main() { foo.Bar("/", router.Index) // Notice caps means its public outside of file } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM