APLawrence - Information and Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds Get APLawrence.com by RSS











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Home > Unix Articles > Google's Go
Printer Friendly Version





Google's Go

Google has introduced Go. Normally, the announcement of a new language isn't likely to get me to even click through to read about it - what do I need a new language for? Can't I get myself screwed up enough with the languages I already know?

Well, the names on the Go design team rung a few bells: Robert Griesemer, Rob Pike, Ken Thompson, Ian Taylor, Russ Cox, Jini Kim and Adam Langley. Yes, THAT Rob Pike, THAT Ken Thompson.

And, what the heck, Google does interesting stuff, so I thought I'd take a look and... well, Go is interesting. There's actually a lot to like.

The first thing I noticed as I worked through the sample code was grouping. In most languages you might do something like this to declare variables and constants:



$counter=0;
$pi=3.1415926;
$Space = " ";
$Newline	= "\n";
 

With Go, you can group related items:


const (
	Space	= " ";
	Newline	= "\n";
)

var (
	countLock	sync.Mutex;
	inputCount	uint32;
	outputCount	uint32;
	errorCount	uint32;
)
 

This isn't a big deal, but it helps make intentions clear, and it does save a little typing.

Go has other tricks to save typing. When you initialize a variable, the compiler works like Perl - it takes the clue from the value you provided. So, while you could say

var s string = "";
 

, you can shorten that to

var s  = "";
 

I like that you can also do

 s := "";
 

because that's the same convention used in loops:

 for i := 0;  < flag.NArg(); i++ {
 

Not a big deal, but it makes sense, so it will be easy to remember, and it also stands out - you KNOW that := is initialization, wherever you see it.

Formatting

Formatting is something i always have trouble with. I'm just not disciplined enough to be consistent. I'm hardly alone - there have been many "pretty print" programs for C and other languages. Go introduces its own "gofmt" program. Unlike general purpose code beautifiers, gofmt knows about Go and won't even touch something it doesn't see as legitimate. Google says:












Formatting issues are the most contentious but the least consequential. People can adapt to different formatting styles but it's better if they don't have to, and less time is devoted to the topic if everyone adheres to the same style. The problem is how to approach this Utopia without a long prescriptive style guide.

With Go we take an unusual approach and let the machine take care of most formatting issues. A program, gofmt, reads a Go program and emits the source in a standard style of indentation and vertical alignment, retaining and if necessary reformatting comments. If you want to know how to handle some new layout situation, run gofmt; if the answer doesn't seem right, fix the program (or file a bug), don't work around it.

As an example, there's no need to spend time lining up the comments on the fields of a structure. Gofmt will do that for you. Given the declaration

type T struct {
    name string; // name of the object
    value int; // its value
}
 

gofmt will line up the columns:

type T struct {
    name    string; // name of the object
    value   int;    // its value
}
 

Ok, small details, but still, that is nice. Remember, the point isn't that a particular beautifier does this or that, it's that gofmt specifically understands Go code.

Function return values

Again, this is no big deal because no matter what the language there's a workaround, but Go directly supports multiple return values. No arrays, no pointers to structures, just:

return x, i;
 

It's obvious, unhidden, far less confusing.

Slices, Maps, comma ok

Go doesn't do pointers. Instead, you pull slices, much like array slices in Perl. So, buffer[10,50] is 50 bytes (or is it 40?) from buffer, starting at its 10th byte.

Maps are like associative arrays:

var weekday = map[string] int {
	"Sun":  0,
	"Mon":  1,
	"Tue":  2,
	"Wed":  3,
	"Thu":  4,
	"Fri":  5,
	"Sat":  6,
}
 

Ordinarily, you'd expect an uninitialized reference to cause some problem or return a null value, and that's true for Go - if you reference weekday["FOO"], Go crashes. But, not if you do it this way:


day, ok = weekday[which]
 

With that syntax, ok is set to boolean "true" if "which" is valid and false otherwise. Easy, right?

Much more

The designers really do seem to have thought things through well. There's much, much more at the link I gave above. I don't think I'll use Go for anything real, but it's fun to see the ideas.

See Google's New Language: Go also.


If this page was useful to you, please click to help others find it:  

Your +1's can help friends, contacts, and others on the web find the best stuff when they search.

2 comments




More Articles by Anthony Lawrence - Find me on Google+



Click here to add your comments



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



ad

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide




 I sell and support
 Kerio Mail server
pavatar.jpg

This post tagged:

       - Google
       - Linux
       - MacOSX
       - Programming




Unix/Linux Consultants

Skills Tests

Guest Post Here