组织Go程序 – 使用包还是其他方式?

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

Organising Go programs - packages or something else?

问题

我已经阅读了Go Tour并搜索了"golang packages",但我还没有找到关于在Go中组织中等规模应用程序的最佳实践的建议。

如果我有一个在概念上有几个不同部分的应用程序,可能有10^3-10^4行代码,并且我不打算创建可重用的库供其他应用程序使用,那么所有的源代码文件都应该是package main吗?


为了澄清...

例如,假设我的程序将有以下主要部分:

  • 管理一组持久存储的数据
    • 允许常规的创建、读取、更新、删除操作
  • 允许人类查看存储的数据的功能
  • 协调/调解这些功能的功能
  • 定期使用SOAP从Web服务获取数据更新的功能。

所以这将是MVC加上一个数据获取器。

从观察别人的做法,我现在怀疑我应该:

  • 创建$GOPATH/src/myprogramname目录

  • 在其中放置一些带有package mainfunc main() { ... }main.go文件。

  • 创建一些子目录,如

    • $GOPATH/src/myprogramname/model
    • $GOPATH/src/myprogramname/view
    • $GOPATH/src/myprogramname/control
    • $GOPATH/src/myprogramname/fetch
  • 在这些子目录中的.go文件中,以package fetch等开头。其中包名始终与子目录名匹配。

  • 我的main.go可能会import ( ... "fetch"; "model"; "view"; "control" )

  • 随着main.go的增长,将其拆分为其他按目的命名的合理大小的.go文件。

  • 通过以下方式构建程序,包括上述包子目录中的*.go文件:

       cd $GOPATH/src/myprogramname
       go build
    

我需要做的就是这些吗?这是正确的Go代码组织方式吗?还有其他我应该了解或考虑的事情吗?有没有一些我忽视了的规范网页或PDF,我应该阅读以了解这些内容?

简而言之,我不想要一个有一万行代码的main.go文件,其中包含所有内容。按照良好的结构化编程和/或面向对象原则,将代码组织成文件、子目录、包和其他组织单元的惯用Go原则是什么?

英文:

I have read the Go Tour and Googled "golang packages" but I have not yet found any advice about best practice in Go for organising moderately sized applications.

If I have an application that conceptually has several distinct parts, perhaps 10^3-10^4 LOC, and I don't intent to create reusable libraries for use in other applications, should all the source code files be package main?


To clarify ...

For example, lets say my program will have the following major chunks:

  • Something that manages a bunch of persistently stored data
    • allowing usual create, read, update, delete operations
  • Something that allows a human to view the stored data
  • Something that coordinates / mediates between these
  • Something that periodically fetches data updates from a web-service using SOAP.

So that would be MVC plus a fetcher of data.

From looking around at what people do, I now suspect I should

  • create $GOPATH/src/myprogramname

  • in there put some main.go with package main and func main() { ... } in it.

  • create some subdirectories like

    • $GOPATH/src/myprogramname/model
    • $GOPATH/src/myprogramname/view
    • $GOPATH/src/myprogramname/control
    • $GOPATH/src/myprogramname/fetch
  • have the .go files in those subdirectories begin with package fetch, etc. Where the package name always matches the subdirectory name.

  • my main.go will probably import ( ... "fetch"; "model"; "view"; "control" )

  • as main.go grows, split it into other reasonably sized .go files named according to purpose.

  • build the program, including *.go in the above package subdirectories by

       cd $GOPATH/src/myprogramname
       go build
    

Is that all I need to do? Is that the properly idiomatic Go way of organising things? Is there more I should know or be thinking of? Is there some canonical webpage or PDF I overlooked and should read to find out this stuff?

In short, I don't want a 10,000 line main.go with everything in it. What are the idiomatic Go principles for organising code into files, subdirectories, packages and any other organisational units corresponding to normal conceptual divisions according to well-known structured-programming and/or OO principles?

答案1

得分: 1

你可以根据函数的封装级别将项目分解为几个层次,例如将低级函数放在单独的包中,将逻辑函数放在主要包中(你可以参考MVC类似的架构)。
由于我们没有关于你的代码的详细信息,很难确定哪种架构最适合。

但最终你的选择将基于代码简洁性和可重用性的平衡。

英文:

You could break down your project into several layers based on the encapsulation level of your functions, i.e. having low-level functions in separate packages and logic functions in your main package. (You could inspire yourself of MVC-like architectures)
Since we don't have any details about your code, it is hard to see what kind of architecture would be best suited.

But in the end your choice will be based on the code simplicity / re-usability balance.

答案2

得分: 0

在Go语言中,一般的最佳实践是每个包提供一种类型或一项服务。标准库中的大多数包都会暴露一种或两种类型,并提供用于处理这些类型的函数。有些包,比如net/http和testing,提供一项服务,但并不是指可以独立执行的"微服务",而是一组与特定活动相关的功能。

英文:

The general "best practice" in Go seems to be having each package provide a type or a service. Most of the packages in the standard library expose one or two types, and functions for working with those types. Some, like net/http and testing, provide a service - not in the "microservices" sense of something executable in itself, but rather a set of functionality related to a specific activity.

huangapple
  • 本文由 发表于 2017年3月9日 19:23:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/42694108.html
匿名

发表评论

匿名网友

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

确定