Go for C programmers


 
Go designers have picked up a lot of good things from other powerful and popular programming languages from the past and the present. Go offers essential tools and mechanisms required for concurrent programming and comes out very strong on that aspects. After many years I really felt a real joy learning a new programming language.

For a C/C++ Programmer learning Go will be a delightful and exciting experience. There are many similarities with C/C++, but beware some things are completely different than the conventions of C/C++!! For example, the lines end without ';' so it's the same feeling when one drives automatic transmission car after driving manual gear-shift cars for many years! Programmers will miss the "break" while writing "switch" code too!

Go is very strict about where "{" is placed. C would happily interpret either of following:

while (1) {
...
}

while (1)
{
...
}

But Go seems to be a bit touchy about it and will accept only first format.

Also, Go ensure that the programmer is serious about what is being written by observing strict economy of the code and will prevent many sorts of sloppy programming. For example, following makes perfect sense to C:

int main() {}

However GO hates it. You can't have a program that doesn't serve any purpose. Same thing goes about unused variables. Go will immediately complain if a variable is defined but not used.

Goroutines


Go offers goroutines as a very powerful way of running concurrent code. For example, let's assume there are two functionally independent code functions f1() and f2() which can run parallelly. One has to just invoke them as:

go f1()
go f2()

A beautiful thing is, if ever required, one can make them behave like traditional routines just by taking away the "go" part. That way they will execute sequentially as before. So turning existing code to concurrent code becomes very easy in Go.

Channels


With concurrent routines comes a necessity to synchronize them. Traditional ways of synchronization like mutexes are provided by Go, but it also provides something very easy to understand and elegant to use - channels.

If you've chanced upon writing programs for Transputers and using OCCAM language, you will immediately see a very familiar concept of a "channel".

A channel can be unidirectional or bi-directional. A channel can act as a synchronisation mechanism between two goroutines. If "c" is a channel which handles integer data, "c <- 2" will write integer 2 to the channel. Reading a channel is equally easy - "n := <- c" will read the channel and assign the read value to n.

Writer process will get blocked if there is no space to write to channel. Similarly a reader process will wait until there is something in the channel to read. This makes synchronization between goroutines very easy.

The _ var


The "_" variable is simply beautiful and goes hand-in-hand with Go's policy of not having a single useless variable in code. If there is a situation where a value (say a return value from a function) needs to be read but should always be ignored, the "_" variable comes very handy. For example, following code as a function body will generate an error in Go:

{
  x := myfunc()
}

So "Go" way to code this is:

{
  _ := myfunc()
}

This is best way to avoid accumulating junk of unused variables over time as code evolves and passes through hands of many programmers.

Other features


C/C++ programmers will find it wierd when it comes to declaring variables. Name comes first, type later - this is further noticeable when declaring array. Same goes while declaring a function - function name comes first and return type comes last.

Go has very simple and few control structures so there is no need to learn many constructs achieving same goal. Assigning values to variables take a compact and efficient form as var1, var2 := val1, val2

Built-in support for advanced features


Go offers ready access to maps, encryption, Client-Server programming etc. using default packages or modules bundled with Go. This is something similar to what modern programming languages offer (e.g. Java). Advantage is that the program development is faster and with considerable less chance of having bugs. A downside could be programmer may never get exposure to intricacies, for example in case of coding Server/Client code.

Summary


So, to conclude, as I said in the beginning, learning Go gives a lot of satisfaction to a hardcore programmer of learning a good and quality programming language. Learning Go is easy, fast and delightful. The compilers are available on all major Operating Systems. Learning material is abundant and available free.

Primary purpose of this article was to give a quick and simple introduction to Go programming language, highlighting some of it's features. This is just  a tip of the iceberg - a lot many beautiful things are still remaining which are not covered here. May be some other time!

Resources


Here is a couple of main websites giving useful information about Go:

1. The Go Programming Language --  https://golang.org/

2. Go information on Wikipedia --  https://en.wikipedia.org/wiki/Go_(programming_language)

I found following books (in given reading order) immensely useful. It is easy to obtain these books from bookstores or from the Internet.

1. The Little Go Book, by Karl Seguin

2. Introduction to Programming in Go, by Caleb Doxsey

3. The Go Programming Language, by Alan A. A. Donovan and Brian W. Kernighan

4. Mastering Concurrency in Go, by Nathan Kozyra

Comments

Popular posts from this blog

Security in Linux Kernel - Part 2

Trusted Platform Module

Common Git Tips