Create a "top level" package. Call it catfacts or whatever else. In this define the things you need to build your app.
packagecatfactstypeSubscriberstruct{Emailstring}// Feel free to tweak method names.typeSubscriberStoreinterface{Find(emailstring)(*Subscriber,error)Create(s*Subscriber)errorList()([]Subscriber,error)Delete(emailstring)error}typeSenderinterface{Send(s*Subscriber,messagestring)error}// In the context of your app, Sender and Retriever are probably descriptive enough. You could also call this FactStore if you want to be more consistent with the SubscriberStore.typeRetrieverinterface{Retrieve()(string,error)}
Now when you want to implement Sender with say Twilio, create a package for that.
I also started writing about this and hope to have more examples in the future, but I'll admit this series has taken the back seat to other work lately: calhoun.io/structuring-web-applica...
There isn't a single "best way" to write apps in Go, but this approach tends to work better for me.
Awesome! Thanks for such an informative response :) . I think when learning a new language it's good to try out different approaches to writing apps since like you said there is no one best way, this was a fantastic look at a way to organize code.
You should definitely experiment a bit. Almost all of the pros/cons of each approach need to be learned firsthand to really sink it, and every approach doesn't fit every problem.
It can be a bit challenging with smaller apps because some of the problems (cyclical deps, etc) won't show up until an app gets to a certain size, but still worth experimenting.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Try this instead:
catfacts
or whatever else. In this define the things you need to build your app.Now when you want to implement
Sender
with say Twilio, create a package for that.Now if you ever want to create a new sender, you just create a new package based on the impl. I just needs to implement
catfacts.Sender
.Back to
catfacts
, you can createSendFactToSubscribers
using your interfaces:Rinse and repeat for any other impls you need - eg
package sqlite
might have an sqlite impl of theSubscriberStore
.This may be a good point to continue this thinking: medium.com/@benbjohnson/standard-p...
I also started writing about this and hope to have more examples in the future, but I'll admit this series has taken the back seat to other work lately: calhoun.io/structuring-web-applica...
There isn't a single "best way" to write apps in Go, but this approach tends to work better for me.
Awesome! Thanks for such an informative response :) . I think when learning a new language it's good to try out different approaches to writing apps since like you said there is no one best way, this was a fantastic look at a way to organize code.
You should definitely experiment a bit. Almost all of the pros/cons of each approach need to be learned firsthand to really sink it, and every approach doesn't fit every problem.
It can be a bit challenging with smaller apps because some of the problems (cyclical deps, etc) won't show up until an app gets to a certain size, but still worth experimenting.