Mastering Go Web Development: Building a Flashcard App
Tags: go, ddd, hexagonal, webdev
Published: 2023-07-23 17:54:47

## Introduction

This project will provide valuable insights into Go's web development capabilities and best practices, whether you're a beginner or an experienced developer.

By the end of this series, you'll have a solid foundation in Go web development and be equipped to create your own web applications

### 1. Installation
First we must install go for your operating system of choice. Im using wsl on windows and have no problem following the [official docs instructions](https://go.dev/doc/install)

We are gonna asume that you are using a go version > 1.16, so modules are enabled by default [(ref.)](https://go.dev/blog/go116-module-changes)

_Note: If you wanna understand the difference between modules and the legacy GOPATH, read [this](https://www.freecodecamp.org/news/golang-environment-gopath-vs-go-mod/)_ great article

### 2. Setting up the project.
_Note: I'm following the convention from [this](https://github.com/golang-standards/project-layout) repository for the project structure._

#### 2.1 Create the folder an initialize the project
```
mkdir flashcard-go
cd flashcard-go
go mod init github.com/<your_username>/flashcard-go
```
This should have created a `go.mod` file. This file has the name of the module and the go version. A module is identified by a <u>module path</u>, which is declared in a go.mod file, together with information about the module’s dependencies. Typically, a module path consists of a repository root path, a directory within the repository (usually empty), and a major version suffix (only for major version 2 or higher)[(ref.)](https://go.dev/ref/mod#module-path)

#### 2.2 Software architecture of the project
There exist a numerous software architecture and patterns, model-view-controller (MVC), client-server, microservices, layered, hexagonal (Ports and Adapters), and various others. In our case i'm choosing a hexagonal approach. It is overkill, it is not neccesary, its impractical for our little project, but it is widely used and we can learn a lot from it.

Our entry points are gonna be inside a cmd folder. Here we can have multiple entry points (for example separate the webapp from the cli) and you could keep everyone on its own folder (for testing purposes). The contents of the files inside this cmd should not be used by other parts of the webapp.

The first thing we are gonna do is separate the core from our source code. As of Go 1.4 the go command introduces a mechanism to define "internal" packages that may not be imported by packages outside the source subtree in which they reside. To create such a package, place it in a directory named internal or in a subdirectory of a directory named internal [(ref.)](https://go.dev/doc/go1.4#internalpackages).

```
flashcard-app/
├─ cmd/
│  ├─ web/
│  │  ├─ // Main entry point
│  ├─ cli/
│  │  ├─ // Possibly another entry point
├─ internal/
│  ├─ // Business Logic
├─ site/
│  ├─ // Front End

```

### 3. Defining the business.

#### Purpose of the App
This webapp is gonna help users create quizzes using flashcards. Users can create, view, edit, and delete flashcards and quizzes. The primary goal is to provide a simple and intuitive interface for users to create educational quizzes.

#### Identify Core Business Concepts

- Flashcard: An individual card used for learning. Each flashcard contains a question and its corresponding answer.

- Quiz: A collection of multiple flashcards organized to test a user's knowledge on specific topics. A quiz should have a title, description, and a set of flashcards.

#### Model Business Rules and Relationships

- Flashcard must have a non-empty question and answer.
- Flashcards are associated with quizzes (many-to-one relationship), meaning multiple flashcards can belong to the same quiz.
- A Quiz must have a non-empty title.
- A Quiz must have a non-empty description.
- A Quiz can have zero or more flashcards associated with it (one-to-many relationship).

#### Entities

- Flashcard
- Quiz

#### Use cases

We are gonna make this thing simple and add these posible uses cases

- Creating, editing and deleting Flashcards
- Creating, editing and deleting a Quiz
- Taking a Quiz: Users can take a quiz, and the application will present each flashcard's question one by one, allowing users to answer
- Scoring a Quiz: After answering all the flashcards in a quiz, the application will calculate and display the user's score

### 4. Start Coding
With the foundation set, we lets begin our coding journey in our next blog post. 

Stay tuned as we delve into implementing the Hexagonal Architecture, organizing the project structure, and bringing our "Flashcard Quiz App" to life using the Go programming language!