duanpin2034 2017-08-13 23:20
浏览 7
已采纳

Go应用程式档案/套件结构

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条回答 默认 最新

  • du958642589 2017-08-14 02:18
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题