Calculator Restful API with GO

In case you are landing here directly, it’s strongly suggested that you go and read through this for fundamentals.

In this blog, we shall be looking at following concepts :- Launching Maths Calculator as Restful API with JSON req/resp in GO.

Question :- Demonstrate the handler that receives a request for a maths operation in a JSON format and sends back a JSON response.

Step #1.) First, we define the following imports And of course, we need to import the encoding through json package.

Step #2.) structures for the request and the response.

  • The request has an operand, like plus, minus, a left and a right side of the operand.

Step #3.) Now, we define the handler :-

  • The handler again receives an http.ResponseWriter and a pointer to a request.

Step #4.) Don’t forget to mount your new handler. This time everyone going to /math on our server, the math handler is going to get called.

Step #5.) Let’s go back to our server and we start it. I’m going to use the Postman to make a request :-

Let’s test it again with some sample value :-

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next series.

References :-

--

--

Software Engineer for Big Data distributed systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store