Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

design patterns - Structuring go project for this use case


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In addition to what @Muhamed Keta writes, I would add the suggestion of using sub-packages for each of the providers instead of one mega-package with 10s of providers in it. This would look like this:

/cmd/app
/config
...
/lib # or /utils 
/providers
    /provider1
        dtos.go
        client.go
        ...
    /provider2
        dtos.go
        client.go
        ...

In go, the package name becomes a namespace and a non-optional identifier/discriminator for client code to use. In this way you avoid having to write longer, redundant and sometimes stuttering names to differentiate from provider to provider:

So in code using your providers instead of writing providers.Provider1Client you'd have provider1.Client. This seems to be a common go pattern/best practice.

This also means that all code that relates closely to the implementation of each provider lives in close proximity, preventing name collisions and aiding developers that focus on a provider exclusively.

I've added a lib (or alternatively utils) package that would contain generic code that may be shared among many providers.

This is subjective, but hopefully I've outlined some reasonable pros given the language's constraints.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...