Review: Google Is On To Something With Go

Google recently made headlines when it announced the creation of a new language. Called Go, the language has already received a good bit of attention. The language is fully compiled in the vein of C++, but includes many types and constructs that make it more similar to something like Python or JavaScript. (Click here to install and learn more about Go.)

I spent some time with Go, and one thing that strikes me is that the compiler is much more intelligent than the older-style, native compilers such as those used for C++. For example, how many times have you wondered: If the C++ compiler can detect that the semicolon is missing, why can’t the compiler just recognise the end of the statement and move happily along? Certainly it’s possible. And the JavaScript parser, for example, is perfectly fine if you leave off a semicolon. And that’s the case with Go. You can leave off semicolons when they aren’t necessary.

You can also let the compiler figure out the type of the variable based on the initial assignment – again demonstrating it’s an intelligent compiler. (For example, if you assign a string constant to a variable, clearly you’re intending that variable to be a string, right? Because if you’re trying to stuff a string into a non-string variable, you’ll get a compiler error.)

I do, however, see a problem with this kind of declaration. It’s too easy to just declare variables haphazardly, on-the-fly, akin to the way people once wrote old BASIC programs. Constructs like this make it easy to write messy code. But I suppose that’s going to be the case in any language. (Remember when people would declare arrays of bytes in C and use the individual bytes for all their storage?)

Go is rich in built-in types, such as those found in most languages. Strings are interesting. Unlike the original C language (which didn’t technically have strings but rather used pointers to array characters to represent strings), Go strings are immutable, meaning you can’t change them. This is actually similar to many modern languages, and is something that bothers some programmers. But it’s just a reality. And it’s not like you can’t change a string variable – you can assign a different string to the variable. You just can’t change the individual members of a string.

Also unlike C, Go arrays aren’t simply glorified pointers. Rather, arrays are an actual type, as they are in other modern language. And as the Go documentation almost apologizes about, the language does have pointers.

The language also has many newer types, such as maps, as well as a special type of “sub-array” called a slice. (The slice concept is new and foreign to a lot of programmers, but they’re pretty flexible and useful. Check out the Go docs to learn more about them.)

Control structures in Go have a rather compact structure to them, and the for statements, for example, include support for iterating through maps. (And there is no while or repeat statement in Go, only a for statement. If that bothers you, I’d say just deal with it.)

When you explore the functions in Go, you really start to see the influence from modern, scripting languages. Functions can return multiple values, just as they can in Python and PHP, for example. In both of those languages, you can return a list, and have the members of the list go right into separate variables in a single line of code. In Go, on the other hand, you can actually specify that a function can return multiple values, like so:

func complex_f1() (re float, im float) {

return -7.0, -4.0

}

And then you can call the function with multiple values receiving the returned values:

x, y = complex_f1()

Then this is interesting: If you don’t want all the values, instead of creating a dummy variable, you can use a “blank identifier” (denoted by a single underscore) to ignore one of the returned values:

x, _ = complex_f1()

The official docs show a rather unique application of this. Check this out:

a, b = b, a

will swap two variables.

Page: 1 2

Jeff Cogswell

Recent Posts

UK’s CMA Readies Cloud Sector “Behavioural” Remedies – Report

Targetting AWS, Microsoft? British competition regulator soon to announce “behavioural” remedies for cloud sector

11 hours ago

Former Policy Boss At X Nick Pickles, Joins Sam Altman Venture

Move to Elon Musk rival. Former senior executive at X joins Sam Altman's venture formerly…

13 hours ago

Bitcoin Rises Above $96,000 Amid Trump Optimism

Bitcoin price rises towards $100,000, amid investor optimism of friendlier US regulatory landscape under Donald…

14 hours ago

FTX Co-Founder Gary Wang Spared Prison

Judge Kaplan praises former FTX CTO Gary Wang for his co-operation against Sam Bankman-Fried during…

15 hours ago