5

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?

1
  • I just found an interesting blog here Commented Mar 7, 2017 at 18:43

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

1 Comment

@Hans, this 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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.