Debugging Go Programming Language | Implementing a binary tree: encoding to json & parallel processing

    Been a while since I took-up exploring Go Lang. The language's simplistic design, quick compilation & efficient concurrency mechanism gives enough substance for the language to be perceived as one of the potential platforms to develop web servers; container platforms(Docker) etc.

Here we go through some of the features which can be capitalized to unleash the true innovation behind simplistic designs. Picking up an example of implementing a binary tree, we explore Go's salient features.

Simplistic web servers                            

Web server, traditionally, is an outcome of some lengthy designs based upon established methodologies/patterns. In the parallel world of Go, web servers can be created and initiated in as small as 3-5 lines of code.

http.HandleFunc unleashes the in-built url routing by Go. Extending to that, we can conveniently route the request URIs based on URL parameter with the help of below code.

Request URI:

       idstr := strings.TrimPrefix(request.URL.Path, "/fetchinfo/")

   id,err := strconv.Atoi(idstr)

Making use of methods from "strings" & "strconv" libraries, we fetch the URL parameter and conditionally decide the request processing and corresponding function execution with http's 'HandleFunc'

http.ListenAndServe in the last line of the below snippet, kick-starts the server at port 1706. With that port setting, we can time-travel to any year we want :D

Self Ref. Pointers                      

The pointer is a very efficient mechanism of transmitting data between functions, routines etc. A simple use-case which follows, highlights mentioning appropriate return types. The Go Playground highlights the efficient usage of 'pointer to struct' as return type of functions in a binary tree implementation & encoding to json. The self reference of struct makes the pointer as return type crucial for the design.

Forming an empty interface of TestObject struct at line # 68 and returning the pointer from the function helps return the content dynamically formed in the implementation (Line # 40) of inserttotree function and encode to json format in line # 70

RESTful and APIs                        

Go enables us to publish any and every sort of content, and expose methods as API. Below is an example of a binary tree generated in the Go Playground published in json.


Parallel processing                     

Go has an efficient mechanism which exploits multi-processor/multi-core environment. Goroutines are lightweight threads managed by the runtime

Take a look above as to how the goroutines, reduce the execution time of multiple iterations of simple functions [taking ~ 1 second to complete] and spread the execution across mutli-processors.

Go Programming opens doors to another bunch of simple, yet powerful features. Example: Multiple return values from a function implemenation etc. We'll continue exploring those in forthcoming posts.

No comments:

Post a Comment