Packaging layout

When we discuss the layout of a Go program, we have a couple of different processes that we should follow. A common convention is to keep your main programs in a folder named cmd. Your other packages that you build to be executed from the main function should live in a pkg directory. This separation helps to encourage the reuse of packages. In the following example, if we want to reuse the notification package for both the CLI and web main programs, we have the ability to do that easily with one import. The following a screenshot shows this separation:

An anti-pattern for Go is to have a one-to-one file for package mapping. We should be writing Go with the idea of driving common use cases together within a specific directory structure. For example, we could create a single directory per file and test it as follows:

Instead, however, we should create our packages as follows:

All of these different notification strategies share a common practice. We should attempt to keep similar functionality coupled in the same package. This will help others to understand any context in which the notifications package has similar functionality.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.194.57