Building Micro-services with GO | Part1
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 :-
- Developing Microservices in GO.
- Creating Http Web Server in GO.
- Read the request body in GO.
- Write the response back in GO.
- Handling Error in Go.
- Accessing the handler from different package in Go.
Question :- Which particular package in GO is most prevalently used for developing Micro-services ?
Answer:- We shall be leveraging the ‘http’ package, which has got huge huge amount of capabilities for building your own web-services.
Question :- Demonstrate the simple server exposing in GO ?
Answer:- Here is how the code looks like :-
Question :- What’s the role of “http.HandleFunc()” ?
Answer:- “http.HandleFunc()” is a convenience method on the GO http package.
- It registers the handler function for the given pattern in the DefaultServeMux.
- It creates an HttpHandler from it and adds it to the DefaultServeMux.
Question :- What’s DefaultServeMux ?
Answer:- It’s a Server Multiplexer and everything related to the server in GO is an Http Handler.
- It is responsible for redirecting the paths. It contains the logic to be able to determine, which particular handler is to be called based upon the path.
- Basically, it determines, which function would get executed whenever a request lands, depending upon where it has been mapped.
- “http” package does have a DefaultServeMux. If we don’t set anything else up, this is the one, which is used by HandleFunc() function.
- It allows you to have multiple handlers.
Question :- What’ happens with “http.ListenAndServe()” function ?
Answer:- When we call “http.ListenAndServe()” which is again a convenience method on the GO http package. It registers a default handler to the server.
- First parameter indicates the Port, where this server shall be hosted.
- Second parameter indicates the Handler as simple as that. If we don’t specify anything
It’s using DefaultServeMux as its route handler and it takes judgement on which code-block gets executed when I have a request.
Question :- Let’s now send a request to the particular URL and see the “http.ListenAndServe()” function in action ?
Here are the logs, we would be observing :-
Question :- How do we now read the body from the request in GO ?
Answer:- We basically make use of a package called as “ioutil”.
Part #1.) Let’s test out our code by supplying the body as well to the request :-
Part #2.) Let’s now have a look @ our code, which shall be reading the body :-
Question :- How do we now re-write back the response in GO ?
Answer:- Again we basically make use of a package called as “ioutil”.
Part #1.) Let’s write some data back to the ResponseWriter :-
Part #2.) Let’s now test out the code :-
Question :- Whats the preferred way to handle errors in GO ?
Answer:- Go’s style of programming is defensive i.e. to make sure we don’t engulf the errors and handle them well. Below are the 2 ways demonstrated to handle the error :-
V. Imp. Question :- In above program, explain what’s happening under the hoods ?
Answer:- It convert the inline-function into an httpHandler and register it into server.
Question :- Let’s reorganise the code as per separation of concerns in GO ?
Answer:- We shall now be separating the handlers in the corresponding handlers package only :-
Step #1.) Let’s create our new package and a file (which shall be hosting handlers exclusively) inside it :-
Step #2.) Now, we would go ahead and start redirecting the http requests to the handler, present into another file :-
Step #3.) Note that, at line #6 above, we have made use of the new package “handlers”. The path is still the full github reference even though Go will reference the local package. It knows that it can do that in the go.mod the module name is starting with github.com..
As a result of above command being executed, we get to now see a file named “go.mod” with following contents in it :-
Step #4.) Let’s now run and execute our server. In Parallel, we shall be verifying by hitting the aforesaid API :-
- In the left-terminal, we have started our server.
- In the right-terminal, we have verified by hitting to the hosted API.
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.