An Introduction to the Go Language: Boldly Going Where No Man Has Ever Gone Before
Develop | Posted June 27, 2013

The Go Programming Language (Go) is an open-source programming language sponsored by Google and created by Robert Griesemer, Rob Pike, and Ken Thompson that aims to simplify systems programming and to make programming fun again. This article aims to introduce developers to Go and give them enough information to try it out for themselves.

Go is a relatively new language that is in constant development but it already has a considerable amount of industry support and is used in real systems at Google.

The notion of yet another programming language may seem weird to some people. But Go does bring something new to the table and its fundamental design principles make it different enough (and arguably, better enough) to justify its existence.

Hello World!

No introduction to any language is complete without the canonical Hello World program:

package main

import "fmt"

func main() {

    fmt.Println("Hello world!")

}

The program defines a new package (main is always the package that contains the main function) then imports the input/output formatting package (fmt), defines its main function (which is the program’s entry point), and uses the Println function from the fmt package to print the string Hello world!.

Why Go?

Go has several features that make it unique and attractive. Here is a non-exhaustive list:

  • Although it is compiled, Go is also a garbage-collected language. This puts less pressure on the developer to do memory management, as the language itself takes care of most of the grunt work needed. In this respect, it is similar to Java, as opposed to C++.

  • Go has built-in concurrency, which allows parallelism in an easier way than is possible in other languages. Go has the concept of goroutines to start concurrent work and the concept of channels to permit both communication and synchronization.

  • Go is a statically-typed language (which makes code more robust as several possible bugs can be detected during compilation) that supports duck-typing (types can satisfy interfaces and be passed in to any functions/methods that accept those interfaces). Go also has type inference, which means you do not need to explicitly mention a variable type whenever it can be inferred by the compiler.

Installing Go

Go can be downloaded from the project page and there are pre-packaged versions for several platforms (Linux, FreeBSD, Windows, MacOS) and architectures (ARM, x86, x64). Full instructions can be found here.

As it is open-source, you can also download its source code and compile it yourself.

Go by Example

From all of the things mentioned above, it should be evident to would-be Go programmers that concurrency support is deeply integrated in the language. This helps making concurrent programming a lot easier. Consider this example from a recent Go talk:

package main

type Ball struct{ hits int }

func main() {

   table := make(chan *Ball)

   go player("ping", table)

   go player("pong", table)

   table <- new(Ball) // game on; toss the ball

   time.Sleep(1 * time.Second)

   <-table // game over; grab the ball

}

func player(name string, table chan *Ball) {

   for {

     ball := <-table

     ball.hits++

     fmt.Println(name, ball.hits)

     time.Sleep(100 * time.Millisecond)

     table <- ball

   }

}

You can run it by yourself and play with it here.

This programs defines a channel (table), starts two players concurrently (“ping” and “pong”), and then starts the game by sending a ball to the table.

Each player tries to grab the ball from the table; only one succeeds. The player that grabbed the ball increments the hit count and sends it back to the table. The other player then grabs it and does the same. This repeats for 100 milliseconds when the main routine grabs the ball again and the program terminates.

Go’s Future

Recently Go 1.1 was released. This version brought significant performance improvements.

Although Go is in constant development, the existing API is stable and guaranteed not to break in any meaningful way throughout all Go 1.x releases. This gives developers a stable platform to work on, which is specially important for large scale projects.

Go is a great language that is improving fast and that showed it can be used for all project sizes. There is already a lot of open-source third-party code that can be used to complete the most varied tasks and a sizeable community to help with all kinds of questions.

Obviously, I’m a fan. Together with everything else mentioned here, Go is definitely an option that you should consider.

See also:

Close

By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox