APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Some material is very old and may be incorrect today

© November 2009 Anthony Lawrence

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 you found something useful today, please consider a small donation.



Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> Google's Go

2 comments


Inexpensive and informative Apple related e-books:

Take Control of Pages

Take Control of Automating Your Mac

El Capitan: A Take Control Crash Course

Take Control of IOS 11

Photos for Mac: A Take Control Crash Course





More Articles by © Anthony Lawrence





------------------------


Printer Friendly Version

Have you tried Searching this site?

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.

Contact us


Printer Friendly Version





Computers are useless. They can only give you answers. (Pablo Picasso)




Linux posts

Troubleshooting posts


This post tagged:

Google

Linux

Programming



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode