CRUD Micro-service with GO

  • Demonstrating the concept of product-api micro-service.
  • Marshalling an object into JSON.
  • Explicitly writing the output to IOWriter.
  • Unmarshalling JSON into an object.
  • Demonstrating RESTful GET API with Go wrt product-api.
  • Demonstrating RESTful POST API with Go wrt product-api.
  • Demonstrating RESTful PUT API with Go wrt product-api.
  • NewEncoder() provides better performance than json.marshal(), as it doesn’t have to buffer the output into an in-memory slice of bytes.
  • This reduces allocations and the overhead of service and therefore the Encoder is marginally faster as compared to the marshal() method.
  • When a micro-service is doing multiple concurrent operations, we must think from the performance prospective. This is just a quick optimisation, and it’s just a standard programming practice.
  • At line #17, we have initialised a new handler, by passing our generic logger object to it.
  • At line #23, we have created a server and the same is using multiple timeouts and custom defined address.
  • At line #31, we have started our server as part of the Go-Routine, which would start the server in an unblocking way.
  • At line #42, we are also listening to the Interrupt / Kill signals on the unbuffered blocking channel. We don’t have any other work to do in this particular main function and therefore we are happy to be blocked on this channel.
  • 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.
  • Note that in below code, at line #98, We are also making use of a utility method called “ToJson()”. The same has also been defined below in the subsequent steps.
  • In below code, at line #17, it indicates that this field would not be returned in this JSON.
  • At line #76, the returnType of the function “GetProducts()” is another struct: Products and we are simply returning the hard-coded list of products having 2 products in it. Simple & straightforward.
  • At line #28, note that the function “ToJson()”, we are basically making use of the NewEncoder() function and encoding the request thus received. In this case, response shall be written onto the ResponseWriter, post encoding.
  • 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.
  • Note that in below code, at line #118, we are making use of service-method “AddSingleProduct()” from the data package. The same has been defined below in the subsequent steps as well.
  • 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.
  • At line #45, we have defined a simple logic for getting the product with last identifier.
  • At line #42, we have appended (i.e. we have added new product to the data-store) to the current list of products.
  • 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.
  • Note that in below code, at line #84, we are making use of service-method “UpdateSingleProduct()” from the data package. The same has been defined below in the subsequent steps as well.
  • 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.
  • Below method “findProduct()” returns three things : product itself, index of that product and Error, if any.
  • At line #63, we are now updating the product in our datastore finally.
  • Note that, we basically updated the product with id value as 2.
  • We also supplied the Http-Verb as PUT using “-X” option.
  • With above operation, we had updated the Name, Description, Price and Sku.

--

--

--

Software Engineer for Big Data distributed systems

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Mistakes I Made CREATING A FPS GAME(and How I Fixed Them)

Arduino — Timers & Interrupts

JDK 18 and the UTF-8 as default charset

Testing systems that communicate with external systems

End to End Unit Testing for .NET 6 Web API

Deploy An Application To App Engine Using Five-Finger Steps

[LeetCode]#1832. Check if the Sentence Is Pangram

Rebalancing Kafka’s partitions

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
aditya goel

aditya goel

Software Engineer for Big Data distributed systems

More from Medium

Simple REST service in Golang with OpenAPI spec and ORM

Generics in Golang.

SQL Injection with GO and Fix

Golang — 1 Minute guide to Useful Tips and Libraries in 2022

Golang — 1 Minute guide to Useful Tips and Libraries in 2022