Let’s go for Google Go!
Technical Feed
Dinesh P B November 4, 2016

In 2007 Robert Griesemer, Rob Pike and Ken Thompson from Google developed a programming language that came to be known as “GO”. Like several other high-level programming languages, GO provides garbage collection, type safety, dynamic-typing capability and many advanced built-in types such as variable length arrays and key-value maps. It is open source, combines the performance and security benefits of using compiled languages like C++ and is really good on networks, web servers and stand-alone command-line apps or scripts. In 1.7 version, it improves the performance in system level programming and GUI based apps or Desktop.

Where does GO language really shine?

Network and Web Servers

The go routine and channel of GO programming is well suited for Network and Web servers. This is one of the reasons why GO projects are mainly applied in networking, distributed functions or services which include API, web servers , minimal frameworks for web applications and the rest.

Stand-Alone Command-Line Apps or Scripts

GO is consistent across various platforms which makes GO exceptionally useful for command line tools and scripts. An executable created for GO is absolutely a stand-alone executable with no external dependencies, whereas one in Python will require an interpreter program in the environment where the Python executable should run.

Another noted advantage Go has here is speed!

Let's go for Google Go!

Advantages of GO

Go is compiled and faster when compared to Python; at the same time it offers dynamic typing features like Python. Despite being a high-level language, it does have low-level features such as pointer-arithmetic, not ruling out the chance of executing Systems and OS programming, is easy to work with and assures high performance. Goroutines are also a powerful abstraction for concurrent programming!

The interface of GO is pretty neat and helps manage large code bases and libraries. The idea is – I don’t really care about the name of something, I care more about what it does, making it possible for objects belonging to multiple directories and designed by different developers to interact!

Getting Started With GO

Official binaries are available for FreeBSD (release 8-STABLE and above), Linux, Mac OS X (10.8 and above),Windows OS and the 32-bit (386) and 64-bit (amd64) x86 processor architectures.

GO Installation

Go is easy to install and configure! You can install GO in Unix systems using the following commands:

“tar -C /usr/local -xzf go1.2.1.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin

If you’re on Windows, download and run the MSI Installer.

When the instal gets completed, you can find the installed files under c:\Go; and the installer would have added c:\Go\bin to the PATH environment variable.

What’s new in GO?

HTTP Tracing

In the latest version of GO, HTTP tracing has been introduced to allow programs to track HTTP related events and life cycle. The package which provide this feature is net/http/httprace. The information obtained can be used for debugging, service monitoring, writing adaptive systems and more.

HTTP events

The httptrace package in GO provides a number of hooks to gather information during an HTTP round trip about a number of events that are connection creation, connection reuse, DNS lookups, writing the request to the wire and reading the response.

Tracing Events

By placing *httptrace.ClientTrace on the requests context, you can enable HTTP tracing in GO. httpRoundTripper reports internal events by looking into the context’s *httptrace.ClientTrace and will call the relevant hook function.

Quoting programs from https://blog.golang.org/ below for a better picture:

req, _ := http.NewRequest(“GET”, “http://example.com”, nil)
trace :=&httptrace.ClientTrace{
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
fmt.Printf(“DNS Info: %+v\n”, dnsInfo)
},
GotConn: func(connInfo httptrace.GotConnInfo) {
fmt.Printf(“Got Conn: %+v\n”, connInfo)
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
if _, err := http.DefaultTransport.RoundTrip(req); err != nil {
log.Fatal(err)
}

Using Subtests and Sub-benchmarks

The latest version of GO offers a Run method on the T and B types that allows the creation of subtests and sub-benchmarks for better handling of failures.

Table-driven tests basics

Let us consider one sample code which illustrate subtests and sub-benchmarks.

func TestTime(t *testing.T) {
testCases := []struct {
gmt  string
loc  string
want string
}{
{“12:31”, “Europe/Zuri”, “13:31”},     // incorrect location name
{“12:31”, “America/New_York”, “7:31”}, // should be 07:31
{“08:08”, “Australia/Sydney”, “18:08”},
}
for _, tc := range testCases {
loc, err := time.LoadLocation(tc.loc)
if err != nil {
t.Fatalf(“could not load location %q”, tc.loc)
}
gmt, _ := time.Parse(“15:04”, tc.gmt)
if got := gmt.In(loc).Format(“15:04”); got != tc.want {
t.Errorf(“In(%s, %s) = %s; want %s”, tc.gmt, tc.loc, got, tc.want)
}
}
}

This approach is referred to as table-driven tests, and it reduces repetitive code.

Smaller Go 1.7 binaries

As GO was designed for writing server programs, its compiler is focused on issues that matter to servers: latency, ease of deployment, precise garbage collection, fast startup time and performance.

Binary size is one of the hiccups being encountered as Go gets used for a wider variety of programs. It has been on the radar for a long time, but the growing interest in using Go for deploying binaries on smaller devices — such as the Raspberry Pi or mobile devices — definitely indicates Go 1.7 release has grabbed the attention of the tech world. Way to go Go!