Thursday, March 28, 2024

Go – Bringing the Fun Back to Programming

Go – Bringing the Fun Back to Programming
What is Go?

Go is an open source programming language that is efficient, expressive and at the same time concise. In short, it makes programmers more productive.

Since Go code is compiled to machine code, it has the speed of a low-level language (like C) however, it also provides you with some of the convenience of higher level languages like garbage collection and the power of run-time reflection. It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

History
Go’s inception goes back to September 2007 on the white-boards of Google where Robert Griesemer, Rob Pike and Ken Thompson were sketching out the goals of a new programming language as a 20% project. In May 2008, Ian Taylor independently started on a GCC front end for Go using the draft specification. Russ Cox joined in late 2008 and helped move the language and libraries from prototype to reality. In November, 2009, Go became a public open source project and ever since, many people from the community have contributed ideas, discussions, and code.

Why Go?

There are so many languages out there, so what’s so cool about Go that I should use it? Well, here’s why…

  • Go supports concurrency at the language level
    Concurrency and multi-threaded programming have a reputation of difficulty. The key reason for this, I believe is because of the complex design and over-emphasis on low-level details such as mutexes and resource locking. Go hides all these complexities from its users (the developers) and implements them under the cover.
  • Go compiles very quickly
    Go compiles to machine code extremely quickly. This might not seem like a huge feature, but from a developer’s perspective, it does make a substancial difference since you need to keep recompiling the source code to to test.
  • Go has garbage collection
    Unlike C and other low-level languages, Go has built-in support for garbage collection. This is a feature that usually comes with higher level languages, but then they become slower too. Go gives you the speed of low-level languages as well as the frills of higher level languages.
  • Unicode Strings and maps are built into the language
    This is one of the few languages that are inherently written in Unicode. String values can be in any language right in your source code. Moreover, having strings and maps built into the language make them quicker to use and become building blocks for higher-level data structures.

How to Go?

At first, Go may stump you with its syntax. It is different from the other languages you would have worked with so far. However, in a few days, you will get used to it… in fact, you might (I sure have) start to find the syntax of other languages unintuitive.

As with all language tutorials, I too will start with a Hello World example. However, you will notice something different with this one.

package main

import “fmt”

func main() {
    fmt.Println(“Hello, 世界”)
}

Yes, Go source code is in Unicode. You can define string literals in any language you like and the standard print function will not spit out junk characters on the screen.

All Go programs must use the main package. You have to define a main function that will initially get called in it. After declaring the package, we need to explicitly specify which packages we will be using in this file. If you specify a package that you don’t use in the file, the compiler will complain that you have imported the package but you’re not using it. One of the quirk of the language that does annoy me a little. The only reason I can think for this is to prevent the code from bloating. If you specify a package that you don’t use, the linker will unnecessarily link it to the binary and in turn slow down the processing.

In the main function, we call the Println function from the fmt package. It works much like the printf in C with the exception that it adds a line break at the end of the output.

This is just a preview of the Go language. We will dive deeper into the good, bad and ugly parts of the language in the forthcoming articles. However, if you can’t wait to get your hands dirty with Go, I suggest you go through the Go Tour. It is an interactive tutorial that lets you test your code right there in the browser.

Where to Go?

Every language has a specific use for it. C/C++ is great for low-level tasks and PHP makes a good language to write websites in. So where should we use Go? The language is primarily geared to handle multiple concurrent conections and pass messages between them. If you’re building a synchronous service or a service where you can run jobs in parallel you should seriously consider Go. It will run fast and the codebase will be incredibly smaller in comparison to its counterparts.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured