Go文件夹结构相同的包

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

Go folder structure same package

问题

让我们假设我想按照以下方式对项目文件进行排序:

- src
    - myProject
        - main.go
        - models
            - foo.go
            - bar.go

我该如何告诉编译器这个结构?目前看起来,所有的文件都必须放在myProject文件夹中,尽管有很多模型,这样很快就会变得拥挤。

有没有一种方法可以在不使用外部包的情况下实现这种结构?(即我仍然希望在models/foo中使用主包)

英文:

Let's say I want to order my project files like this:

- src
    - myProject
        - main.go
        - models
            - foo.go
            - bar.go

How can I tell the compiler about this structure, at the moment it looks that all my files have to be in the myProject folder, although with quite a few models, this will get crowded really quickly.

Is there a way I can achieve this structure without having to use external packages? (i.e. I still want to use the main package inside models/foo)

答案1

得分: 5

go工具假设每个包对应一个目录。

你可以直接运行编译器和链接器来实现任何你想要的布局。因为没有人这样做,所以你会发现支持这种方法的文档很少。

随波逐流。使用多个包。

英文:

The go tool assumes one directory per package.

You can run the compiler and linker directly to achieve any layout you want. Because nobody does this, you will find that documentation to support this approach is slim.

Go with the flow. Use more than one package.

huangapple
  • 本文由 发表于 2015年1月31日 01:14:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/28241132.html
匿名

发表评论

匿名网友

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

确定