Go app file/package structure

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

Go app file/package structure

问题

我正在构建一个命令行应用程序,它导入一些数据(例如来自数据库、CSV 或 XML),并将其导出到其他格式(例如导出到数据库或文件)。在启动应用程序时,可以通过输入参数指定导入器和导出器。

目前,我将每个导入器和导出器分别放在自己的文件中。每个导入器都满足一个通用的导入器接口:

type Importer interface {
    Import() ([]*data, error)
    PreProcess() error
    PostProcess() error
}

因此,例如 CSVImporter 将实现这三个函数。导出器也是类似的。目前,所有的导入器和导出器都属于主包,与主文件放在一起。我认为这是最合理的,因为它们是应用程序的一部分,不应该在其他项目中使用。此外,由于我有很多配置设置(一些是通用的,一些是针对每个导入器和导出器的),在主包的开头进行了解析,我认为我的导入器和导出器应该是同一个包的一部分,这样我就可以在那里使用这些设置。

package main

type ConfigFile struct {
    Environment    string
    Resources      *Resources
    Importers      *Importers
    Exporters      *Exporters
    ...
}

func main() {
    var cfg ConfigFile

    // 加载配置文件设置
    err := viper.Unmarshal(&cfg)

    var i StockImporter
    var e StockExporter

    // 创建所选的导入器
    switch viper.GetString("importer") {
        case "csv-importer":
            i = NewCSVImporter()
            ...
    }
}

我的当前文件结构如下:

app
|- main.go
|- importer.go
|- csvimporter.go
|- xmlimporter.go
|- exporter.go
|- csvexporter.go
|- lib
    |- db.go

希望我对当前结构的描述清楚明了。我的问题是:是否有更好的方法来组织我的应用程序结构?具体来说,我不喜欢将所有文件都放在与 main 文件相同的目录中。有什么建议吗?谢谢!

英文:

I'm building a CLI app that imports some data (e.g. from a db or CSV, XML) and exports it to some other format (e.g. to a db or file). An importer and exporter is specified when the app is started as input parameters to the app.
I'm looking for some inspiration on how to structure the files/packages.

At the moment, I have separated each importer and exporter in its own file. Each importer satisfies a general importer interface:

type Importer interface {
    Import() ([]*data, error)
    PreProcess() error
    PostProcess() error
}

So, for instance, CSVImporter would implement these 3 functions. Likewise for the exporters. Currently all importers and exporters are part of the main package together with the main file. I think this makes the most sense, as they are part of the app, and should not be used in other projects. Also, since I have a lot of config settings (some general and some for each importer and exporter), that are unmarshalled in the beginning of my main package, I believe my importers and exporters should be part of the same package, in order for me to use the settings there.

package main

type ConfigFile struct {
    Environment    string
    Resources      *Resources
    Importers      *Importers
    Exporters      *Exporters
    ...
}

func main() {
    cfg ConfigFile


    // Load config file settings
    err := viper.Unmarshal(&cfg)

    var i StockImporter
    var e StockExporter

    // Create selected importer.
    switch viper.GetString("importer") {
         case "csv-importer":
         i = NewCSVImporter()
         ...
    }
}

My current file structure looks like the following:

app
|- main.go
|- importer.go
|- csvimporter.go
|- xmlimporter.go
|- exporter.go
|- csvexporter.go
| - lib
    | - db.go

I hope my current structure is described clearly. My question is: Are there better ways to structure my app? Specifically, I dont like having all the files in the same dir as main. Any suggestions?
Thanks

答案1

得分: 2

请看一下如何编写Go代码

根据文档,这是一个实际工作区的结构示例:

bin/
    hello                          # 可执行命令
    outyet                         # 可执行命令
pkg/
    linux_amd64/
        github.com/golang/example/
            stringutil.a           # 包对象
src/
    github.com/golang/example/
        .git/                      # Git仓库元数据
	hello/
	    hello.go               # 命令源代码
	outyet/
	    main.go                # 命令源代码
	    main_test.go           # 测试源代码
	stringutil/
	    reverse.go             # 包源代码
	    reverse_test.go        # 测试源代码
    golang.org/x/image/
        .git/                      # Git仓库元数据
	bmp/
	    reader.go              # 包源代码
	    writer.go              # 包源代码
    ... (省略了许多其他仓库和包) ...

我可能会创建一个CSV和一个XML包,并在其中放置它们对应的导入器/导出器,或者有一个导入器包和一个导出器包。

更新:

建议的项目结构 -

app
|- main.go
|- config
    |- config.go
|- importer
    |- importer.go
    |- csv.go
    |- xml.go
|- exporter
    |- exporter.go
    |- csv.go
    |- xml.go
英文:

Take a look at How to Write Go Code

From the documentation, this is how a workspace should look in practice:

bin/
    hello                          # command executable
    outyet                         # command executable
pkg/
    linux_amd64/
        github.com/golang/example/
            stringutil.a           # package object
src/
    github.com/golang/example/
        .git/                      # Git repository metadata
	hello/
	    hello.go               # command source
	outyet/
	    main.go                # command source
	    main_test.go           # test source
	stringutil/
	    reverse.go             # package source
	    reverse_test.go        # test source
    golang.org/x/image/
        .git/                      # Git repository metadata
	bmp/
	    reader.go              # package source
	    writer.go              # package source
    ... (many more repositories and packages omitted) ...

I would probably create a CSV and an XML package, and put their corresponding importers/exporters there, or have an importer and an exporter package.

Update:

Suggested project structure -

app
|- main.go
|- config
    |- config.go
|- importer
    |- importer.go
    |- csv.go
    |- xml.go
|- exporter
    |- exporter.go
    |- csv.go
    |- xml.go

huangapple
  • 本文由 发表于 2017年8月14日 07:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/45665698.html
匿名

发表评论

匿名网友

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

确定