Jviaches Blog

Since we working with Go, it’s good time to play a bit with Fibonacci numbers. First of all it’s great in case of learning a new programming language and second, we will try to analyze a bit recurrent function and write it’s short iterative implementation. As a bonus we will write function that return function that return int .

But let’s starts from a theory. The definition of Fibonacci numbers state : Fn = Fn-1 + Fn-2 . The whole idea is find number by given its index.

Note: Simple well known recursive approach. The first problem is possibility of stack overflow. The second one it’s exponential time of execution. Third problem is repetitive calculations. For example if we will calculate value at index 4 -> f(3) + f(2), it mean that f(2) will be calculated twice. And if we will calculate value at index 5 -> f(4) + f(3), it mean that f(3) will be calculated 3 times. And so on..

So suitable solution for third problem may be using some data structure (slice for instance) to store data and avoid unnecessary calculations.

2. Iterative function.

There are a short mathematical representation developed by Binet based ongolden ratio:

Since we working with Go, it’s good time to play a bit with Fibonacci numbers. First of all it’s great in case of learning a new programming language and second, we will try to analyze a bit recurrent function and write it’s short iterative implementation. As a bonus we will write function that return function that return int

But let’s starts from a theory. The definition of Fibonacci numbers state : Fn = Fn-1 + Fn-2 . The whole idea is find number by given its index.

Note: Simple well known recursive approach. The first problem is possibility of stack overflow. The second one it’s exponential time of execution. Third problem is repetitive calculations. For example if we will calculate value at index 4 -> f(3) + f(2), it mean that f(2) will be calculated twice. And if we will calculate value at index 5 -> f(4) + f(3), it mean that f(3) will be calculated 3 times. And so on..

So suitable solution for third problem may be using some data structure (slice for instance) to store data and avoid unnecessary calculations.

2. Iterative function.

There are a short mathematical representation developed by Binet based ongolden ratio:

In this post i will introduce some basic (real basic) web development in go. We will start to implement TODO list and at the end of this topic it would’t be perfect but it will compile and show us proper results. May be in following topics we will continue to develop and expend this project. Sources will be available at the bottom of this post.

Here we declare all packages we going to use. It right time to mention that code will not compile when you try to declare some package you are not going to use. By the way the same approach with defining unused variables.

Next we define ToDo item as struct (since i go there is no classes) and some TodoItemsSlice as slice. In short, slices are collections : it has an ability to add and remove items.

Lets take a look in main andrunServer functions: In main function nothing special: printing to console regarding server initialization and creating new instance of empty slice. The running runServer function

It’s easy to see that server starting listening on port 8080 (browser port). In addition we can find two handlers: one for entering for the “localhost” and the second one when entering to “localhost:8080/additem” (in order to add more items in ToDo list).

Now let’s skip default handler and talk about addHandler. In general handlers are functions that will executed when user will enter to specific URL. More about Go handlers you can find here.

Method addItemHandler gets two parameters: reponce and request. When use will enter to “localhost:8080/additem”, this handler will be executed. So first line is printing in console (by using log) and the we parse our view file by usage oftemplate. Usually view file can be represented as html but n this case it represented by gtpl which very similar to aspx file in APS.NET.

Interesting point here is that template.ParseFiles function returns two values (yes, it’s possible in go ) – result and error. Result represented by “t” variable, but we not going to use error at this point. If we define it (error variable) we have to use it, otherwise it will not compile. In order to avoid usage of returned variable we just using “_” telling Go that we are not interesting in it.

Another interesting point here is defining variables. In go there are two ways to define variable: by using “var” and by using “:=”. You can read about it here.

At the beginning we trying to get caption value which we read from form. If found one, we create new todo item and populate it with proper data and append it to the slice. And the simply returning to the main.gtpl and showing results.

At this point i was encountered with this specific issue and it took me some time to get to the point and make it work. Since i spent some time on it, i suppose there are other who will encounter in the same issue. So let me show you how i resolved it.

Goal: output dynamically changed slice on the main.gtpl page.

Approach: within main handler we pass to the Execute function our slice which actually will be a context of main.gtpl. That’s it!

* Please let me know about any issue you encountered with (not compiling, not clear some point or untipattern, etc..)

Most of the day we spend answering and sending emails. Therefore, correctly configured email box can increase your productivity and free up the time for other aspects of the work. There are couple options to make our work with Gmail with maximum convenience:

Undo send. It gives us ability to cancel almost send email in specified cancellation period of time, which in some cases might be useful.

Undo Send email in gmail

Stars. By adding more stars we can get more control over our email discussion. Short glance, quicker response.

Adding more stars in gmail allow you to get more control over you email conversations

Signature. Allow us to complete email without reentering "regards" manually

Gmail signature

Labels. Free you visual space by hiding unnecessary items.

Show - default option. Label will be shown

Hide - label will be hide even if it contains unread emails

Show if unread - means hide by default, but if it contains unread emails, it will be shown.

A large part of my nature is to constantly move forward in spite of everything: of failure, ups and downs, joy or sorrow. I constantly learning from anything i experience. I look at it the same way as the natural process of breathing or a natural smile on face of a standing smiling man.

Since the constant movement always creates friction, I always have to look for new inspiration that will give me energy in my travels for constantly movement forward.

And this time I discovered a new direction – a motivational video. And here is the first one.

I think I falling in love for the first time after years of software development. I believe it should happen at least once to each software engineer, who truly loves what he does.

It wasn't love from the first sight. I felt something strange, something completely different from knowledge i gain till this point. But sometimes people prefer their warm and very familiar comfort zone. And this was the case why our date was over then.

Since i remember my career, I always have been developed in OOP: C#, Java/Android. And any other approach felt like i have to leave cozy comfort zone of mine for something unfamiliar and risky, something questionable to invest my free time. I have a wife and lovely twins after all.

By the way i really encourage starting from there, since it's the best overview to get familiar with Go concepts and approaches. Clear explanation along with great code examples will provide you great starting point.

Anyway i was amazed by Go's different paradigms, its approaches and most of all from idea of language that was created with taking concurrency in mind. Actually its the right time to explain little bit what i have discovered:

Some Facts:

Language was developed in 2007 by Google engineers.

Was announced by Google in November 2009

It is complete opensource.

Go supported by Google Cloud

Pros:

Great build-in documentation and lot of libraries out-of-the-box

Garbage collection, type safety and some dynamic capabilities

Avoid stop-the-world GC pauses with no delays

Have no classes at all

Intentionally designed without any inheritance at all. Use composition instead.

Function may return more then one value. Such ability does not exist in C# or Java.

Working with Go routines (represent thread) is really easy and have ability to exchange data between them by using channels.

There is no possibility to use undefined variables.

Interesting idea of visibility : function or member starting with capital letter become visible to other packages (public modifier in C#). Small letter indicate no visibility from outside (private modifier in C#)

Some thoughts at this moment:

It looks to me Go has a lot of potential. Language itself do efforts you write cleaner and understandable code by applying it's rules. For example there is no possibility to use undefined variables, what is possible in C# and leads to less readability and message.

Since Go become part of Google's infrastructure it smells like relative stability to me. Besides, company has a lot of experiences in enterprise software development as well as in concurrency fields, meaning they try to implement their extensive knowledge in Golang.

Although it feels different from what i used to, but looks like going to invest a lot of time with this language.

We all worked with ContinueWith() method but there are some interesting and useful facts we should be aware of.First of all lets remember what is Continuation regarding the Tasks. When we looking for this definition in MSDN , we see following:

Creates a continuation that executes asynchronously when the target Task completes.

But actually there are few interesting facts :)Fact 1: task1 and task2 in previous example may run on a different threads. In order to obligate to run on the same thread we can use TaskContinuationOptions.ExecuteSynchronously.Fact 2: Its important to know that ContinueWith() method will be executed anyway, whenever Task succeeded, faulted or canceled. It give us great opportunity to deal sometimes with different cases accordingly: in case of exceptions do this and in case of success do that. We can achieve it like this:

Please notice to parent.Wait() method. Its says to the parent that it will be completed when everything inside it is completed.
If you will try to run without mentioned earlier string, the results will be "slightly" different.

Output:

Now we would like to execute 2 sons tasks within father task. We can achieve it like following (as expected)

Notice that code is not define who run first within father's task. It only says that father task will complete only after everything else is completed inside it.

To explore:

Let say that we second son should be executed right after first one is completed. So how do we achieve it ? MSDN suggest us to use TaskCreationOptions.PreferFairness. I will cover it in my future posts.