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.

Categories
Uncategorized

The “Go” Programming Language: A First Look [Part-1]

It’s been a while I am looking at Go. The language is nice, feels like a mixture of Python and C with huge performance gains and some innovative language features. In this post, I would try to quickly focus on the basics of “Go”. I hope I shall write more on the language in the coming days.

Installation

Installation is really platform specific. I would happily forward you to http://golang.org/doc/install for installation instructions on your OS.

If you happen to have the same OS as mine, that is OSX, I highly recommend installing Go using Homebrew. It’s just a matter of one line –

Once it’s installed, you can display useful information with this command –

If you are on Windows, Linux or any other OS, please follow a suitable instruction set from the documentation.

Running Go Programs

How does a first program look on Go? Let’s see –

A Go file must define a function named main if you want to run it. We can define a function in go using “func” keyword.

Running it –

You can directly run a Go file by using the “go run” command. This comes in handy for debugging, if you want to build a compiled binary, use “go build”.

Variables, Constants and Printing

First, how do we define variables in Go? It’s simple –

In Go, we can import packages using the “import” keyword. The package “fmt” has a nice function named “Println” which is more or less Go equivalent to System.out.println() in Java. Let’s see a code sample –

If you are initializing a variable, you can optionally skip the variable data type. Go can infer that from the initialization.

There is a shorthand format for declaring and initializing a value quickly –

It will create a var type string and assign the value “masnun” at the same time.

To create a constant, we just replace “var” with “const”. Example:

Looping

In Go, we use “for” for all types of looping. Check out –

While Loop:

Generic For Loop:

We can do common “foreach” loops combining “for” with “range”. We shall see them in the next section.

Arrays, Slices and Maps

Arrays:

If you are any programmer at all, I probably don’t need to tell you about arrays. Defining arrays in Go is easy.

The syntax is –

We can reference the indexes just like in any other language. We can even declare and initialize an array on the same line. Take a look at the example:

Slices:

Slices are like arrays but not fixed size. However, the type must remain the same. I mean you can’t add a string type to an integer slice.

We use the make() function to create a slice. In fact, in Go, make() can create a lot of stuff for me. We shall soon find out. For now, let’s see how we can use a Slice.

Maps:

Maps are what we call “Hashes”, “Dictionaries” or “Associative Arrays” in misc other languages. Maps are in reality key value pairs. We use make() to create maps of certain types. Example follows –

Iteration with Range

“range” allows us to iterate over a number of data structures. range gives us the key and value in each iteration. It works quite like the “enumerate” function in some languages (eg. Python). Let’s see how we can iterate over common data structures:

That’s all for Part – 1. In my next blog post, I shall try to cover “If/Else” with “Structs, Functions & Methods”.

Categories
Uncategorized

Beautiful Themes for Sublime Text 3

I have been using ST3 for a while now on test drive and I have checked out some of the themes currently available for Sublime Text 3. All of these themes are available via Package Control. I am also going to link the screenshots to their respective github repo in case you don’t want to use Package Control for some (weird) reasons.

Soda Theme

Soda is probably the most popular theme for Sublime Text

Flatland Theme

Cobalt 2

Nexus Theme

Reminds me of Google Nexus.

Nexus Theme

Pseudo OSX theme

Mac / OS X users are going to love this one

Reeder Theme

Here’s another nice one

Phoenix Theme

And it’s cool

Centurion

Which one did I miss?

So you know there’s a cool theme for ST3 that is not yet on this list? Please let me know in the comments, I shall add it.