3 ms·
I'm a novice at Go, but I've written several email libraries in other languages. Two lessons from that experience that I thought I'd pass on: - I've found it
by curveship 13y ago
I'm a novice at Go, but I've written several email libraries in other languages. Two lessons from that experience that I thought I'd pass on:
- I've found it a more flexible architecture to separate the send functionality off from your email object itself and into its own service class. So instead of doing email.Send(), you do emailService.Send(email). The content of an email, and how to send that content, are separate responsibilities. Separating the two also allows for alternate implementations of the Send functionality: one (like here) that does a synchronous SMTP transfer, one that might drop the message into an internal queue so that client code doesn't need to wait for the (potentially fairly slow) SMTP transfer, one that simply appends to a local file for debugging, etc. (These are all examples from real apps.)
- Have you considered making required properties (to, from, subject) properties of the constructor? Doing so makes guaranteeing that they're set a compile-time check rather than a runtime check, which can make a library more robust and suss out bugs earlier.
- jwcrux 13y agoThanks for the suggestions! 1) I worked to create the package such that other sending services can easily be used to send the email. By calling the .Bytes() function, you will get a slice of bytes which is the raw email representation that's meant to be sent across the wire. I only included the Send() function as a helper. 2) I'm actually not sure if there's a way to do this in Go.. I suppose I could have made the NewEmail() function take arguments for required items - and I did think about doing this- but what arguments? In what order? Using previous libraries, I've always had to go look up the docs every time I use them because I forget what order the to and from are supposed to be in. Currently, only runtime checks are performed to make sure there is at least one from and to addresses given. Thanks for the suggestions - I really appreciate it!