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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?