4

I've got a Go library that I'd like to distribute. It's got 62 source files, but I'd like to keep the API in a single package. Is there a way I can use multiple directories for code in a single package? It's not a huge amount of source, as the source files themselves are small, and I'd like to keep it that way if possible to make it navigable.

Since it was modeled after similar libraries in other languages, It's currently got two packages: /project and /project/models. After learning more about Go packaging I now realize that this is unwieldy for distribution and use. The user would prefer a single package.

Is there a "gopheric" way of doing this?

Hans
  • 2,230
  • 23
  • 23
  • I just found an interesting [blog here](https://medium.com/@benbjohnson/structuring-applications-in-go-3b04be4ff091#.iq41404je) – sigit_prayoga Mar 07 '17 at 18:43

2 Answers2

5

Read this two articles to have a good understanding of the best practices:

  1. Go In Production
  2. Organizing Go code

Each path directory can be a separate module/package in your app:

github.com/myaccount/myapp/
    README.md
    Makefile
    applib/
        cmd/
            main.go
            main_test.go
            handlers.go
            handlers_test.go
        lib1/
            main.go
            main_test.go
            process.go
            process_test.go
        lib2/
            foo.go
            foo_test.go
            bar.go
            bar_test.go
fabrizioM
  • 46,639
  • 15
  • 102
  • 119
  • 1
    @Hans, [this](http://stackoverflow.com/a/15051192/720999) explains the approach to having many "private" packages "inside" the main one. I'm using quotes because Go does not have neither private nor nested packages but it's very easy to obtain the same level of functionality. – kostix Apr 29 '14 at 07:56
3

No, it is dead simple: One directory == one package. But having some kind of "main" package with "helper" packages in subdirectories is not really "unwieldy for distribution" as go get handles this very well.

Volker
  • 40,468
  • 7
  • 81
  • 87