2018-01-30 10:11
浏览 23

将一个主程序包放在cmd / myapp子目录中可以吗?

this is my first Go project and I have had a hard time grasping the different documents on code organization (e.g. I found it somewhat hard to tell which ones have been superceded, or apply to deprecated vendoring tools/approaches). I finally settled on Ben Johnson's Standard Package Layout, with a very slim root package (in the root directory of my project) and most code in internal/pkg/...

However, I've added a cmd/myapp dir and put my main.go file for the executable program in there, since I understood that to be a common pattern.

So now I have:

+- myapp.go   // "package myapp", with only type and interface declarations and no imports
+- cmd
|   |
|   +- main.go // "package main", with import gitlab.tld/username/myapp, gitlab.tld/username/myapp/internal/pkg/routing etc.
+- internal
    +- pkg
        +- routing
        |    |
        |    +- routing.go // "package routing"
        +- sql 
        +- etc.

Now, when I go to cmd/myapp and call go build, it builds fine and leaves an executable that does what it's supposed to do right there. But when I call go build -v from the project's main directory, no executable is produced. In fact, only myapp.go seems to be processed which contains only type definitions. (And of course, go install does not produce a binary either. go get -v gitlab.tld/username/myapp also does not seem to do anything.)

So I have the suspicion that this is not how it is meant after all, but I am not even so sure about that. Is there a problem with the project layout, or did I get the usage of go tooling wrong?

Thanks for any help you might provide to this go newbie,


图片转代码服务由CSDN问答提供 功能建议

这是我的第一个Go项目,我很难掌握有关代码组织的不同文档(例如,我发现 很难说出哪些已被取代,或适用于已弃用的供应商工具/方法。 我终于选择了本·约翰逊(Ben Johnson)的标准包装布局,它的根很细 包(在项目的根目录中),大多数代码在internal / pkg /...

中,但是,我添加了cmd / myapp目录并将main.go文件放入 因为我知道这是一种常见的模式,所以它在那里的可执行程序。


 +-myapp.go //“打包myapp”,仅包含类型和接口声明,而没有导入
 |  | 
 |  +-main.go //“包主”,带有导入gitlab.tld / username / myapp,gitlab.tld / username / myapp / internal / pkg / routing等。
 +  -pkg 
 |  | 
 |  +-routing.go //“包路由” 

现在,当我走时 到cmd / myapp并调用 go build ,它可以正常运行,并留下可执行文件,可以执行应有的操作。 但是,当我从项目的主目录调用 go build -v 时,不会生成可执行文件。 实际上,似乎仅处理myapp.go,其中仅包含类型定义。 (当然, go install 也不生成二进制文件。 go get -v gitlab.tld / username / myapp 似乎也没有任何作用。)

所以我怀疑这毕竟不是它的意思,但是我什至对此不太确定。 项目布局是否存在问题,或者我误用了go工具?

感谢您为go go新手提供的任何帮助, \ n


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhao8233 2018-01-30 10:59

    When you run go build or go install the command expects a list of packages to process. If no import paths are given, the action applies to the package in the current directory.

    So if there is no package main in your root directory, no executable will be produced.

    The command doesn't look in subdirectories. You may have multiple commands in your cmd directory. In this case it won't know which one to build. And building all of them every time is wasteful.

    解决 无用
    打赏 举报

相关推荐 更多相似问题