A Practical Look at Go, Part 1

1 Comment

By Ken Walker Senior Software Engineer

About two years ago, I was working on a project written in the Go language. Go was originally developed by Google in 2007 for internal use, but was later released (open source) for general use. The project I was on was a large enterprise-wide service that collected large amounts of data (and did it well). This article introduces some of the interesting aspects of the language that might entice you to consider it for your own use, while pointing out a few things of which to be aware if you do.

General Considerations

Go code runs on Linux, Windows, and OS X. Recently, Go also began providing support for ARM processors and people have been experimenting with Go running on Apple and Android smartphones.

Designed as a systems language, Go is intended for server-side development. However, nothing prevents it from being used for client-side programs. Indeed, some GUI libraries have been published to try to help with this approach.

As an open source project, Go comes with all the associated pros and cons:

On the plus side, the Go community is large and active. Folks have been writing a lot of new tools and libraries even since the time I was using Go. (Check out the long list at Awesome Go.)

On the other hand, you may have to do some searching around to get the right combination of versions (between the Go language and the libraries you want to use) that work with each other. Your experience won’t be as smooth as using monolithic vendor development environments (think Microsoft .NET). But even those are never 100% smooth anyway. With some patience and a start-up attitude you can reap the benefits.

When searching for information about Go, searching for “golang” is much more fruitful than searching for just “go”.

Concurrency

One of the key features that drove the development of Go was being able to spawn threads quickly and easily. Hence the go keyword:

func main() {
    go SpawnMe()
}

That’s all it takes to spin the SpawnMe() function off in a separate thread.

Channels

A channel provides a pipeline for passing data from one thread to another.

ch1 <- 13 // Send the value 13 into channel 1
x <- ch2  // Receive a value from channel 2

You can create a channel for any type of data, including structs.

By default, a channel has no capacity and the sender and receiver will block until both are present at the channel. This is convenient behavior when you want to synchronize the two threads. Alternatively, you can create a buffered channel that has a storage capacity. Here, the sender will not block as long as there is an available slot and the receiver will not block as long as there is a message waiting to be received.

Go also provides a select statement that allows you to wait on multiple channels at one time. If no message is available on any of the channels, the thread will block until one appears at one of them.

while (true) {
    select {
        case x <- chHome:
            print(“Got “, x, “ from home.\n”)
        case y <- chSpace:
            print(“Got “, y, “ from outer space.\n”)
        case <- chQuit:
            print(“Done listening.\n”)
            break
    }
}

Notice the final case which is a channel specifically created to send a message when all operations have been completed. Its value is discarded because we don’t care what the value is; just that we got one. This is a common idiom in Go.

Multiple Return Values

A function or method in Go can return multiple values. This removes the need to whip up special data structures or constantly pass pointers to variables just to be able to update multiple items.

x, y, z, err := GetNextValues()

Raising Errors

There are no exceptions to be thrown in Go. Notice the variable err in the previous example. This is how you flag errors in Go—include an error code as one of the values returned. A function or method that returns an error code expects you to check it before using any of the other returned values.

Like many design choices in the programming industry, there is quite a bit of debate about which is the best mechanism for communicating errors. If you’re interested in the Go designers’ rationale for supporting only error codes, see this article.

Next Time…

In part 2 of this article, we will explore Go’s approach to object orientation and some of the impacts this would have on your daily programming life.

In the meantime, if you’re already intrigued, here are some places you can start doing your own research:

A Tour of Go, which introduces you to the basics of the language. It includes playgrounds where you can edit and run Go programs related to the topics being discussed.

The Go Programming Language Specification.

Packages, documenting all the standard packages that come with Go.

Projects, which can help you begin to explore the other Go-related resources out there.

Author: bridge360blog

Software Changes Everything.... Bridge360 improves and develops custom application software. We specialize in solving complex problems at every phase of the software development lifecycle, removing roadblocks to help our clients’ software and applications reach their full potential in any market. The Bridge360 customer base includes software companies and world technology leaders, leading system integrators, federal and state government agencies, and small to enterprise businesses across the globe. Clients spanning industries from legal to healthcare, automotive to energy, and high tech to high fashion count on us to clear a path for success. Bridge360 was founded in 2001 (as Austin Test) and is headquartered in Austin, Texas with offices in Beijing, China.

One thought on “A Practical Look at Go, Part 1

  1. Pingback: A Practical Look at Go, Part 2 |

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s