CRUD Micro-service 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 :-

  • Demonstrating the concept of product-api micro-service.

Question:- Demonstrate the micro-service which shall be exposing the method to fetch products-list ?

Answer:- We want the products (i.e. list of products) to be returned through the API Call.

Step #1.) Here is how the model for products looks like. We can also specify the different names of the fields in JSON output that we might want OR we may just omit it 100% as shown below :-

Step #2.) Here is the function, which shall be returning the array of products :-

Step #3.) Here is the list of products, that we are returning. In practical world, this would come from database or some source of truth :-

Step #4.) Package “encoding/json” would allows us to marshall a struct OR Reference into JSON. Here is how the same has been done :-

Step #5.) Here is our main function, where we have defined the server and concerned handler :-

Question:- Is there a way to write output directly to IOWriter ?

Answer:- Encode() method writes JSON encoding directly to the io.Writer. It’s doing exactly the same what marshal() is doing, but instead of returning the json data to the console, it’s writing directly to the io.Writer.

Question:- Why would I just NOT use the standard marshal() ?

Answer:- It’s because of following reasons :-

  • NewEncoder() provides better performance than json.marshal(), as it doesn’t have to buffer the output into an in-memory slice of bytes.

Question:- Let’s now break the entire code using HttpVerbs for this particular Micro-Service ?

Step #1.) Let’s start with our main program. Here are set of dependencies we have mentioned into our main program :-

Step #2.) Here is how the code of our main function looks like :-

  • At line #17, we have initialised a new handler, by passing our generic logger object to it.

Step #3.) Here is how our handler function looks like. Note that, in above step #2, we are using the “NewProducts” method, in order to create an object of the Products handler.

Step #4.) Inside the Handler file, let’s now write this particular method “ServeHTTP()”. With this, we are checking on HTTP-VERB, if the verb being supplied is GET, then we use the function “getProducts()”, which in-fact is the part of this very same handler class. The same is defined in step #5.

Step #5.) Here is how the code of function “getProducts()” looks like.

  • Note that in below code, at line #97, we are making use of service-method “GetProducts()” from the data package. The same has been defined below in the subsequent steps.

Step #6.) Here is how the code of service-method “GetProducts()” from “data” package looks like :-

  • In below code, at line #17, it indicates that this field would not be returned in this JSON.

Note :- At line #10 above, we have defined a Struct, which have some identifiers with it. Below defined are the meanings to the various fields and tags defined in the STRUCT :-

Step #8.) Let’s now execute the code so far and test the output :-

Step #9.) Back inside the Handler file’s ServeHTTP() method, we also check on HTTP-VERB, if the verb being supplied is POST, then we use the function “addProduct()”, which in-fact is the part of this very same handler class. The same is defined in step #10.

Step #10.) Here is how the code of function “addProduct()” looks like.

  • Note that in below code, at line #111, We are also making use of a utility method called “FromJson()”. With this method, we are reading a JSON and unmarshalling the same to an object. The same has already been defined above in step #6.

Step #11.) Here is how the code of service-method “AddSingleProduct()” from “data” package looks like :-

  • In below code, at line #40, we are fetching the next-identifier to be used with the record (product in this case)to be added to the local data-store.

Step #12.) Let’s now re-run the code and verify, whether POST is working :-

Meanwhile, let’s again fetch the products from our Micro-service. We should now have three products :-

Step #13.) Back inside the Handler file’s ServeHTTP() method, we also check on HTTP-VERB, if the verb being supplied is PUT, then we use the function “updateSingleProduct()”, which in-fact is the part of this very same handler class. The same is defined in step #14.

In below code, we are trying to extract the path-parameter from the URL, in order to know the IDENTIFIER against whom, the object is required to be updated.

Step #14.) Within the handler class, here is how the code of function “updateSingleProduct()” looks like.

  • Note that in below code, at line #77, We are also making use of a utility method called “FromJson()”. With this method, we are reading a JSON and unmarshalling the same to an object. The same has already been defined above in step #6.

Step #15.) Here is how the code of service-method “UpdateSingleProduct()” from “data” package looks like :-

  • In below code, at line #67, we are fetching the product (which has to be updated) from the underlying datastore. Recall that, we are also checking whether ID of the product received through the URL is same as that of record through which we are iterating.

Step #16.) Let’s now re-run the code and verify, whether PUT is working :-

  • Note that, we basically updated the product with id value as 2.

Let’s now again get the products from the micro-service using GET verb and here are results :-

Finally, here is how our go.mod file looks like :-

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