英文:
Golang - How must I arrange my workspace for a package with a sample program?
问题
我还在努力理解Go工作区布局。我有一个名为 todinfo
的包和一个使用它的示例程序 untod
。
我目前的目录结构如下:
$GOPATH
+- bin...
+- pkg ...
+- src
+- github.com
+- longborough
+- (其他文件夹)...
+- todinfo
+- todinfo.go
+- untod.go
我最初是在不同的目录中开发这两个程序的。然而,由于 untod
实际上是 todinfo
包的一部分,将它打包成一个单独的项目,放在同一个目录中似乎更合理。
但是当我尝试安装时(为了清晰起见,我将回复分成了三行):
D:\Development\Go\src\github.com\longborough\todinfo>go install
can't load package: package github.com/longborough/todinfo:
found packages todinfo (todinfo.go) and main (untod.go)
in D:\Development\Go\src\github.com\longborough\todinfo
我希望我弄错了,但这有点像Java,至少对于初学者来说是这样。
我做错了什么?我应该使用哪些Go命令来安装包并安装示例程序?或者,正确的目录结构是什么?
英文:
I'm still trying to get my head around the Go workspace layout. I have a package, todinfo
and a sample program which uses it, untod
.
I currently have this arrangement of directories:
$GOPATH
+- bin...
+- pkg ...
+- src
+- github.com
+- longborough
+- (others)...
+- todinfo
+- todinfo.go
+- untod.go
I originally developed these two programs in separate directories. However, since untod
is really part of the todinfo package, it seems more sensible to package it up as a single project, in the same directory.
But when I try to install (I've wrapped the reply onto three lines for clarity):
D:\Development\Go\src\github.com\longborough\todinfo>go install
can''t load package: package github.com/longborough/todinfo:
found packages todinfo (todinfo.go) and main (untod.go)
in D:\Development\Go\src\github.com\longborough\todinfo
I hope I'm mistaken, but this smells a bit like Java, at least to the uninitiated.
What am I doing wrong? What Go commands should I use to install the package and then install the sample? Or, what is the correct directory arrangement?
答案1
得分: 1
这让我也有点困惑。可以这样理解:untod
不是todinfo
包的一部分,它是todinfo
包(库)的使用者。实际上,main
根本不是一个包,只是一个标记,表示它有一个入口点,并且应该编译成一个可执行文件。
简而言之:你可以把untod
放在任何地方。放在根目录可能是合理的选择:它将以你的$GOPATH
的最后一个目录组件命名。或者,如果你有多个可执行文件,可以将其放在cmd/untod/untod.go
中。
在进一步开发之后,你可以考虑创建一个单独的存储库,比如github.com/longborough/todinfo-bins
,以将它们分开。
英文:
This got me too. Think of it this way: untod
is not part of the todinfo
package, it's a consumer of the todinfo
package (library). In fact, main
is not really a package at all, just a marker to say that it's got an entrypoint and should be compiled into a binary.
TLDR: you can put untod
anywhere. In the root is probably sensible: it will then be named whatever the last dir component of your $GOPATH
is. Alternatively, put it in cmd/untod/untod.go
if you've got multiple binaries.
After some more development, you might consider making a separate repo like github.com/longborough/todinfo-bins
to keep them apart.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论