In this project, I am creating web-based calculator application in Golang. During implementation, I have followed MVC (Model-View-Controller) architectural pattern for separating the logic of each module.
The program is organized in three packages:
-
Model
-
Calculator:
This class contains the main logic of application. In our case, it implements all core functionalities (like addition, subtraction, etc.) of calculator.
-
-
Controller
-
CalculatorController:
This class mainly responsible for calling specific struct methods from Model package based on user request, and updating view at the same time. In our application, user will call
PerformOperation()
method of this class for every operation by clickingsubmit
button.PerformOperation()
method decides which method to call from Model struct.By this way, user will not directly interact with structs inside Model package. Instead, it will only interact with structs inside Controller package.
-
-
View
-
CalculatorView:
This struct contains necessary variables used while displaying calculator. Values of this struct's membor variables are initialized and updated by
CalculatorController
.This struct also contains
ShowCalc()
method, which executescalculator_view.html
file and loads the current object for displaying existing values. -
calculator_view:
This HTML file is used for User Interaction. It will show values to user by loading
CalculatorView
struct variables.Each button requests different methods from
CalculatorController
struct. So, user will interact with Controller by filling up and submitting the form.
-
-
Main
Main program instantiates objects of structs from Model, View and Controller packages. Then, it reserves specific port (9090 in our case) for running the application.
- make view package for UI and building html template in it.
- unit testing.
- error handling for preventing bad user input.
- Implementing the similar functionalities using REST API.
- Implementing Design Pattern once this project gets many functionalities to handle.
First of all, you will need to clone this repository using following command:
git clone https://github.com/rachitt96/calculator_design.git
cd calculator_design
Now, there are two ways in order to run this application as explained below:
-
Installing Go manually
In this method, we will need to ensure that Go is installed in the local machine. If it is not installed, then we will need to download and install specific installer based on machine's operating system.
Check if Go is installed by running following command:
go version
Use following command in order to run this application after installing Go:
go run main_program.go
-
Using Docker container (recommended)
In this method, we don't need to worry about installing Go for a specific OS, but we need to ensure that Docker Desktop is installed.
Run the following command in order to build the linux container for our application:
docker build -t go_web_calculator:latest .
Run the following command to run our container and binding the port 9090, which is used by our application.
docker run --rm -p 9090:9090 go_web_calculator:latest
Type this URL in web browser.
localhost:9090