avatarIsrael Josué Parra Rosales

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3291

Abstract

bc1">In this section, we delve into the packages essential for our use case. Initially, we thoroughly examine the packages recommended by Golang, offering a comprehensive description of each. These packages are pivotal in shaping the structure of our project.</p><p id="4a56">Simultaneously, we examine the architectural framework of the microservice under choice for this chapter — the “Shopping Cart.” This meticulously designed structure serves as a blueprint for the other microservices that are part of our use case.</p><h1 id="bec1">Golang standard package that will be used by our solution</h1><p id="74f5">In a Go project, the directories named <b>cmd</b>, <b>internal</b>, and <b>pkg</b> are common conventions used to structure code and modularize it, serving a specific purpose in project organization:</p><h2 id="1fe0">cmd</h2><p id="3c97">The <b><i>cmd</i></b> (stands for “commands”) directory is used to house the application’s executable entry points. Each subdirectory in <b><i>cmd</i></b> could correspond to a separate tool, utility, or program in your project. Each of these subdirectories can have its own main.go and specific dependencies. These subdirectories act as executable “commands” in the project and are the main input for starting different parts of the application.</p><h2 id="2631">Internal</h2><p id="7477">The <b><i>internal</i></b> directory is used for packages that should only be accessible from the module in which they reside. This means that these packages are only for internal use within your own project and should not be imported by other external projects. This structure helps maintain tighter encapsulation and reduces coupling between different parts of your application.</p><p id="9c2c">In other words, the code inside <b>internal</b> is meant to be private to the project itself.</p><h2 id="8d13">pkg</h2><p id="8c3b">The <b><i>pkg</i></b> directory is similar to the <b><i>internal</i></b> directory but with one key difference. The packages inside <b><i>pkg</i></b> are designed to be exported and used by other external projects.</p><p id="f77c">If you have reusable components or utilities that may be useful to other projects, you can place them in this directory. These packages can be imported and used in other projects thanks to the use of go modules.</p><p id="788d">It is important to note that these conventions are widely accepted in the Go community, but are not mandatory. You can customize the structure of your project according to your needs and preferences. However, following these conventions can make your code more clear, maintainable, and understandable to other developers who are familiar with these common practices.</p><h1 id="966b">The microservices packages based on DDD</h1><p id="fa01">The Domain-Driven Design (DDD) structure that will be implemented to organize our code is based on the following packages:</p><div id="71ab"><pre>root-folder/ ├── cmd/ └── <span class="hljs-keyword">internal</span>/ ├── app/ │ └── api │ ├── dto │ ├── handlers/ │ └── services/ ├── domain/ ├── infrastructure/ │ ├── persistence/ │ └── database/ └── shared/ ├── logger └── errors/</pre></div><p id="661a">Let’s describe a bit each one of the pa

Options

ckages inside of internal:</p><h2 id="42d2">internal/app/api</h2><p id="e692">This package houses the application-specific logic, including controllers, services, and any other components that are directly related to the implementation of the application. It provides an intermediate layer between the high-level logic (domain) and the technical details (infrastructure), allowing business logic to communicate with external interfaces (controllers, APIs, etc.).</p><h2 id="f925">internal/app/api/handler</h2><p id="bbd7">Contains the controllers that handle HTTP requests or user input and turn them into actions in the application.</p><h2 id="2701">internal/app/api/services</h2><p id="1399">In this package reside the application services, which implement the business logic. Services can be made up of more complex operations that involve interactions between various entities.</p><h2 id="f7dd">internal/app/api/dto</h2><p id="8978">These DTOs encapsulate specific data required for concrete operations and can include validations and transformations. Furthermore, they are documented and tested to ensure their proper functioning. Their main objective is to enable a clear separation of responsibilities and enhance efficiency in communication between application layers.</p><h2 id="a1c8">internal/domain</h2><p id="397c">This package contains the main entities of the domain. It defines and encapsulates the core business logic in data structures with related behaviors. In this package, you will find implementations such as entities, value objects, services, etc.</p><h2 id="e088">Internal/infrastructure/persistence</h2><p id="a55a">This package is used to implement the repositories that interact with the storage layer (database, memory, etc.) to retrieve and store data. This package abstracts access to the storage layer and allows data access concerns to be separated.</p><h2 id="29e8">internal/infrastructure/database</h2><p id="3a44">This package contains the specific implementation for connecting and accessing the database.</p><h2 id="cc67">internal/shared/errors</h2><p id="5f11">Defines custom errors that can occur in the application. They can include application errors (for example, incorrect user input) and business errors.</p><p id="8512">This organizational structure follows the principles of DDD, where business logic is separated from technical concerns and organized into entities, aggregates, services, etc. The modular and well-structured approach makes it easy to scale and maintain as the project grows.</p><p id="8fe5">Next readings …</p><p id="b5b2">Wait for Chapter 11“Building a Microservice With Go — Coding our Microserivice ”.</p><div id="6644" class="link-block"> <a href="https://readmedium.com/chapter-11-codding-our-microservice-part-1-16c40ceec5b1"> <div> <div> <h2>Chapter 11 - Coding our Microservice (Part 1)</h2> <div><h3>Building a Microservice With Go</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*BDDoHo-cSuSCAP1UjKc1QA.jpeg)"></div> </div> </div> </a> </div></article></body>

Chapter 10-Structuring our project

Building a Microservice with Go

The following list is the previous chapters of this series:

I recommend you take a look at the previous chapters if you have not read them yet. That will help you to get more knowledge in this wonderful world of “Microservices architecture”.

Structure

In this chapter, the following topics will be covered:

  • Structuring our project
  • Importance of a good code Structure
  • Adding a software architecture to our microservices

Introduction

Organizing software code within a project is paramount to maintaining order, comprehension, and scalability as the project evolves. A robust structure fosters collaborative efforts among developers and streamlines the integration of novel functionalities.

It’s important to note that the specific arrangement may diverge based on programming language, framework, and team conventions. Above all, consistency and intuitiveness are paramount, serving to uphold the project’s tidiness and facilitating its manageability amidst expansion.

Throughout this book, our focal point will be on the microservices of Orders, Shopping Carts, and Users. This approach ensures a contextual abstraction, rendering concepts and associated codes readily graspable.

The uniform code structure will be shared mainly across microservices, leveraging elements of Domain-Driven Design principles alongside the standard Golang packages.

Defining packages

In this section, we delve into the packages essential for our use case. Initially, we thoroughly examine the packages recommended by Golang, offering a comprehensive description of each. These packages are pivotal in shaping the structure of our project.

Simultaneously, we examine the architectural framework of the microservice under choice for this chapter — the “Shopping Cart.” This meticulously designed structure serves as a blueprint for the other microservices that are part of our use case.

Golang standard package that will be used by our solution

In a Go project, the directories named cmd, internal, and pkg are common conventions used to structure code and modularize it, serving a specific purpose in project organization:

cmd

The cmd (stands for “commands”) directory is used to house the application’s executable entry points. Each subdirectory in cmd could correspond to a separate tool, utility, or program in your project. Each of these subdirectories can have its own `main.go` and specific dependencies. These subdirectories act as executable “commands” in the project and are the main input for starting different parts of the application.

Internal

The internal directory is used for packages that should only be accessible from the module in which they reside. This means that these packages are only for internal use within your own project and should not be imported by other external projects. This structure helps maintain tighter encapsulation and reduces coupling between different parts of your application.

In other words, the code inside internal is meant to be private to the project itself.

pkg

The pkg directory is similar to the internal directory but with one key difference. The packages inside pkg are designed to be exported and used by other external projects.

If you have reusable components or utilities that may be useful to other projects, you can place them in this directory. These packages can be imported and used in other projects thanks to the use of go modules.

It is important to note that these conventions are widely accepted in the Go community, but are not mandatory. You can customize the structure of your project according to your needs and preferences. However, following these conventions can make your code more clear, maintainable, and understandable to other developers who are familiar with these common practices.

The microservices packages based on DDD

The Domain-Driven Design (DDD) structure that will be implemented to organize our code is based on the following packages:

root-folder/
├── cmd/
└── internal/
    ├── app/
    │   └── api
    │      ├── dto
    │      ├── handlers/
    │      └── services/
    ├── domain/
    ├── infrastructure/
    │   ├── persistence/
    │   └── database/
    └── shared/
        ├── logger
        └── errors/

Let’s describe a bit each one of the packages inside of internal:

internal/app/api

This package houses the application-specific logic, including controllers, services, and any other components that are directly related to the implementation of the application. It provides an intermediate layer between the high-level logic (domain) and the technical details (infrastructure), allowing business logic to communicate with external interfaces (controllers, APIs, etc.).

internal/app/api/handler

Contains the controllers that handle HTTP requests or user input and turn them into actions in the application.

internal/app/api/services

In this package reside the application services, which implement the business logic. Services can be made up of more complex operations that involve interactions between various entities.

internal/app/api/dto

These DTOs encapsulate specific data required for concrete operations and can include validations and transformations. Furthermore, they are documented and tested to ensure their proper functioning. Their main objective is to enable a clear separation of responsibilities and enhance efficiency in communication between application layers.

internal/domain

This package contains the main entities of the domain. It defines and encapsulates the core business logic in data structures with related behaviors. In this package, you will find implementations such as entities, value objects, services, etc.

Internal/infrastructure/persistence

This package is used to implement the repositories that interact with the storage layer (database, memory, etc.) to retrieve and store data. This package abstracts access to the storage layer and allows data access concerns to be separated.

internal/infrastructure/database

This package contains the specific implementation for connecting and accessing the database.

internal/shared/errors

Defines custom errors that can occur in the application. They can include application errors (for example, incorrect user input) and business errors.

This organizational structure follows the principles of DDD, where business logic is separated from technical concerns and organized into entities, aggregates, services, etc. The modular and well-structured approach makes it easy to scale and maintain as the project grows.

Next readings …

Wait for Chapter 11“Building a Microservice With Go — Coding our Microserivice ”.

Golang
Programming Languages
Software Development
Software Architecture
Software Engineering
Recommended from ReadMedium