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

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