Calculator

Report a typo

Implement a web server that includes a calculator using router parameters. The calculator should support four actions: /add (addition), /sub (subtraction), /mult (multiplication), and /div (division).

Following the action name, define two numbers that the action should be applied to. For example, if the request is made to the route /add/2/2, the server should respond with the number 4. Note that all arithmetic operations should be performed with numbers of type int.

After defining the routes and endpoints, launch a web server that listens on the address 127.0.0.1:8080.

Tip: You can define a route with its own handler for each arithmetic operation. For example, router.GET("/add", ...). Alternatively, you can define the arithmetic operation as a URI parameter (:action, for example), and determine the action name using an if-else or switch-case construction.

Write a program in Go
package main

import (
"fmt"
"io"
"net/http"
"time"

"github.com/gin-gonic/gin"
)

type Calculator struct {
// your code here
}

func main() {
// DO NOT delete the two lines below! They disable Gin's debug mode to check your solution
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard

// your code here

go func() { // DO NOT delete this line of code
// Start the web server at "127.0.0.1:8080" below:
router.Run(?)
}()

check() // DO NOT delete this line of code
}
___

Create a free account to access the full topic