Golang – 我应该如何为一个带有示例程序的包来安排我的工作空间?

huangapple go评论110阅读模式
英文:

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,以将它们分开。

Dave Cheney在这个主题上给出了一些建议。

英文:

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.

Dave Cheney has some decent advice on the subject.

huangapple
  • 本文由 发表于 2016年2月2日 00:10:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/35135081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定