Categories
Uncategorized

Vim Madness

I use the top notch IDEs for development but that is not geeky enough. So, ignoring my previous failed attempts, I am going to give the text editor one more try. Apparently, this blog post would be more like self documentation, mostly notes and hints to self. If anyone else also gets benefited, that’d be an added bonus.

Quick Shortcuts

  • Vundle Bundle Installation: “:BundleInstall”
  • New Tab: “:tabnew
  • Navigate Tabs: “:tabn” for next, “:tabp” for prev. “gt” in normal mode
  • Switch Windows: “Ctrl + w, Ctrl+w”
  • Splitting Windows: “:split “, “vsplit” for vertical splits
  • NerdTree – Open in new tab: “t”

Let there be ~/.vimrc

OK, here’s my ~/.vimrc, the initial version is messy, not organized at all, everything I came across, I stuffed in here. I shall update and clean the vimrc over time.

Categories
Uncategorized

Go: Creating new types based on built-in types

Ever needed a custom string type to add your own methods or tweak certain behavior? In languages like Ruby or Javascript, you can directly modify built in types. In other languages, we usually create custom objects to wrap around a default type and then extend that object with necessary methods or attributes.

A sample example in Python would look like:

In Go, we would define a new type based on an existing type. Then define methods on the newly defined type. It allows us to easily create new types based on existing types and then customize to suit our needs. The same example in Go would look like:

Simple, isn’t it?

Categories
Uncategorized

Goroutines: Threading made easy!

This post is dedicated to the awesome concept of “Goroutines” in Go. A “Goroutine” (Go Routine?) is a lightweight thread implementation that executes asynchronously with the main process. To define a goroutine, we use the “go” keyword before a function call. Instead of a regular blocking call, the function is then executed asynchronously.

Let’s jump into a quick demonstration. We shall write a program that creates multiple threads, each thread sleeping for a random period of time. For us, the mere mortals, we shall politely ask the threads to print some information so that we can tell when they go to sleep and when they wake up!

The blocking call (from the main process) always executes first. The Scanln() call waits for user input thus allowing the async functions to print their information on the terminal. If we don’t wait, the main process will finish execution and the program will quit. Since the other functions are async, we shall never see their outputs. So we wait and wait for their messages! 🙂

Sample output?

It’s simple, right? In some languages, you have to subclass a built in class and then define specific methods to direct the behavior/actions. In Go, you just define a function and prepend “go” before making the function call. Good thing, you can use anonymous functions and invoke them immediately as well.

For communication between these goroutines, we can use “Channels”. Hope to write another blog post on using channels soon.