dongzha2525 2014-05-23 21:21
浏览 61

组织代码的惯用方式

I feel the pending 'close question, too vague, opinions,...' but here goes.

How do I organize my go codebase?

I have a non trivial app (unix daemon) that talks to other processes, read/writes a database, talks to a web server, has internal state. In c++ i would write a bunch of classes (probably in subdirectories for the main subcomponents). Lets say that this is projd

Then at a higher level I have utility cli functions associated with the project. projcli1, projcli2.... I assume all the code associated with one project goes under one GOPATH

What If i was working on several unrelated projects. Do I still use one GOPATH or do I have one per project.

I tried to find sample code bases but the only large set is the packages repository, and that's a bunch of libraries - which is not really the same thing.

If those questions are too vague how about this. Is it correct that all the go files in the same directory have to be for the same package?

Another simple specific question. I found a nice implementation of an object pool. Its package says "pool" - fine. Where do I put its single file. In a subdir called 'pool', I would like to have a dir called utils (or something like that) seems like I cannot do it. I mean I cant have utils/pool and util/db (say) without having a tree of one file dirs

  • 写回答

1条回答 默认 最新

  • duanju8431 2014-05-23 22:45
    关注

    Your question is fairly vague, but you do ask a few answerable questions in it.

    First, read http://golang.org/doc/code.html if you haven't already

    You should only ever need one GOPATH; you can have several and switch between them, but I can't think of any use case for it.

    All go files in a single directory must be for the same package, that is enforced by the compiler.

    When organizing a large project in Go, it's all about choosing your packages and deciding what code goes in each package. A few useful tips:

    • package dependencies cannot be cyclic - if your theoretical layout would have two packages depending on each other, that will not work
    • subdirectories are entirely distinct packages - they have no special relationship to the package in their parent directory
    • each package should be independently useful - if you have to import package b in order to make use of the stuff in package a, your layout is confused
    • don't be scared of relatively large packages - it's weird at first to have lots and lots of go files in a single directory (where in other languages you'd be using subdirectories) but it doesn't actually cause too many problems

    For large projects of the size you seem to be talking about, I find they usually end up in three or four packages: one or two implementing stand-alone core components that could theoretically be reused in other projects, one implementing the specific application functionality as a library, and one implementing the actual program (the daemon, or the user interface, or what-have-you). It always depends on the program of course.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值