Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internal organizing for golang package with many files

Tags:

package

go

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?

like image 404
Hans Avatar asked Mar 19 '23 18:03

Hans


2 Answers

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
like image 71
fabrizioM Avatar answered Mar 25 '23 03:03

fabrizioM


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.

like image 23
Volker Avatar answered Mar 25 '23 04:03

Volker