Go language from Google, a Ford T with a Ferrari engine
Google has created the Go language to operate with multi-core processors of today, that is difficult with current languages. As always, this new language was created in response to the dissatisfaction caused by the shortcomings and disadvantages of current languages. In this case, it was intended to replace C++ for low level programming, that neither Java nor C# can do. But that does not mean it is actually used for that. It is amusing that the creators have wanted to keep a syntax that programmers are accustomed to, that of C, but the majority use Go to replace Python!
Modern features and 40 years old syntax!
To design it, several veteran of programming were hired, and that explains the legacy syntax of the language...
- Ken Thompson. Co-author of UNIX, creator of the B language which was followed by C (by Dennis Ritchie).
- Rob Pike, created the Plan 9 system for Bell.
- Robert Griesemer has contributed to the V8 JavaScript compiler, Chrome and GCC.
These developers have nothing to do with C++, which was created by Bjarne Stroutstrup on the basis of the C language, which explains different choices of design, such as composition instead of inheritance.
The goal of Go is to stay as close as possible to the C language while significantly improving productivity and replace it.
It borrows features from C, Java, Pascal, Python and even incorporates features from Scriptol!
Google says in the presentation:
"We claim that Go is a modern language."
And it's true for its features, not for the syntax which dates exactly from 1969 and the B language, and with some borrowing from Pascal (1970)!
It is used in production at Google since May 2010, but beside the fact that it replaces C++ very effectively, it also tends to be used in place of Python or Ruby because its compilation speed is suitable for scripting, while it produces fast binary executables. It so can replace all languages for command line programs.
Another quote, from the author, Rob Pike:
The key point here is our programmers are Googlers, they’re not researchers. They’re typically, fairly young, fresh out of school, probably learned Java, maybe learned C or C++, probably learned Python. They’re not capable of understanding a brilliant language but we want to use them to build good software. So, the language that we give them has to be easy for them to understand and easy to adopt.
Why use Go? The user experience...
The experience is rather negative among individual programmers, having used other modern languages. Go is primarily designed for a team where adding / replacing a programmer is recurring. It allows to be rapidly productive even for beginners.
Its syntax and the way it treats objects intend it primarily for programs on servers as a replacement to Perl, Python or PHP. It then allows both a fast development and quicker scripts.
Has builtin concurrency and garbage collector.
The use of a garbage collector is generally not suitable for the making of an OS, drivers, but it has been specially optimised to achieve pause of less than 100 microseconds!
It may be used to make server software and for example to build a CMS and generates HTML pages, a field where it is superior to Python or Java.
It is simpler than C++, more adaptable than Java, quicker and safer than Python, has an extensive (server side) and well designed library and provides the services required to Web applications, such as WebSocket, closures.
And programs are compiled instantly which facilitates the development, as does the clear description of errors it displays too, unlike other languages such as. C++.
A syntax from the C language
The syntax improves productivity.
- No semi-colons required but to separated statements.
- map is a hash table and is part of the language.
- Multiple threads that communicate with each other.
- Variables are declared as in Pascal:
var name type
Ex: var x int; - break and continue may specify a label.
- The switch case structure may include different types or comparators as in Scriptol.
- A case statement can contain several alternatives:
case 0, 1, 2: - A slice is a structure that includes a part of a list and point to it.
- The go command calls a function by starting a different thread.
- chan is a channel for communication between goroutines (which are functions of concurrent processes).
- UTF-8 format for strings.
- Primitives are: bool, string, int, int8 to 64, the same for uint, byte, float32 and 64, complex64 and 128.
CSP (Communicating Sequential Processes) manages communication between program with support for multi-core processors.
Go vs. Java
Java was designed as a language for the Web and became popular thanks to that. It can run on the server or the client with applets (the latter are now obsolete).
Its main advantage is its huge library to build graphical user interfaces.
Go as Java allows you to test a program or script development immediately, but it produces binary code, so is faster and more compact.
Go vs. C++
Even if it uses a syntax of the 70s, programming is simplified by Go compared to C++.
Some common causes of errors coming from the syntax are removed.
Multi-threaded operation becomes quite easy thanks to a single command.
The garbage collector eases memory management.
- There is no class and inheritance. Classes are replaced by structs, and interfaces which are close to objects of JavaScript. Objects are extended by composition rather than inheritance.
- Memory is managed automatically by a garbage-collector. In October 2016 it was announced that the new GC pauses last less than 100 microseconds, therefore invisible to the user.
- Pointers have a type set.
- Arrays are passed by values and not pointers.
- Imports of packages like in Java rather than C headers.
- No type conversion without using a function.
- nil replaces null.
- The pointer symbol -> is replaced by a dot.
- Increments -- and ++ can not be used in expressions.
- Constants can be declared without a type.
- new is a function and not an operator.
- Go has no generics, because that has lot of drawbacks, according to the author (Ref: Esmeralda imagination).
Comparing syntax...
Go
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
C++
#include <stdio.h>
void main() {
puts("Hello, World!");
}
Go: two equivalent declarations.
var i : int = 10
var i = 10
C++
int i = 10;
Go
var str = "Hello in Go"
fmt.Println(str)
C++
string str = "Hello in C++";
puts(str);
Go
const a = 1
C++
const int a = 1;
Go
var b int = 2
var x, y, z int
var i, j, k int = 5, 10 , 20
b += x
C++
int b = 2;
int x, y, z;
int i = 5, j = 10, k = 20
b += x;
Go
x := 2
C++
int x = 2;
Go
if x < 1 {
fmt.println("ok")
}
if x = mult(y, 100); x < 1000 {
fmt.Prinfln("ok")
}
C++
if(x < 10) {
puts("ok");
}
x = mult(y, 100);
if(x < 1000) {
puts("ok");
}
Go
switch x {
case "a":
fmt.Println("a")
case "b":
fallthrough
case "c":
fmt.Println("b or c")
default:
fmt.Println("x")
}
C++ 11
switch(x) {
case "a":
puts("a");
break;
case "b":
case "c":
puts("b or c");
break;
default:
puts("x");
}
Go
for i:= 0; i < 10; i++ {
fmt.Println(i)
}
C
int i;
for(i = 0; i <= 10; i++)
puts(i);
Go
var i = 0
for i < 10 {
fmt.Println(i)
i += 1
}
C
int i = 0;
while(i < 10) {
puts(i);
i += 1;
}
Go
for {
if condition {
break
}
}
C++
while(true) {
if(condition)
break;
}
Go
var arr = []int { 5,10,20 }
for i, v := range arr {
fmt.Println(v)
...
}
C++ version 11
int arr[] = { 5,10,20 };
for(x : arr) {
puts(x);
...
}
Go syntax to declare a function is simple, while Rust being unnecessarily complicated and stupid.
If several arguments have the same type, the type is declared once. But they remains static in the two languages and do not offer multiple dispatch as in Julia.
Go
func mult(x int, y int) int {
return x * y
}
C++
int mult(int x, int y) {
return (x * y);
}
Go
func ftuple(x int, y int) (int, int) {
return x * 2, y * 2
}
x, y = ftuple(10, 15)
C++
int[] ftuple(int x, int y) {
return { x * 2, y * 2 };
}
int z[] = ftuple(10, 15);
x = z[0]; y = z[1];
Go
type Point struct {
x int
y int
}
C++
struct Point {
int x;
int y;
}
Go
package main
import ("os";"flag";)
func main() {
var s = "Demonstration"
for i := 0; i < s; i++ {
fmt.Println(s[i])
}
}
C++
#include <stdio>
#include <string>
void main() {
string s = "Demonstration";
for(i : s) {
puts(i);
}
}
In terms of libraries, Go goes directly against C++, not only there is no file header, but you can even include external modules directly from a remote site, because all the information for the integration is stored in the modules.
In general that facilitates the distribution of programs, but it has also disadvantages. Modules imported directly cease to be compatible and require rewriting the software.
Controversy about the name
At the public launch of the language, a ticket has been created on the forum of the language on issue 9.
The author of an totally unknown language claims that the name Go is already that of his own language.
The fact is that a book has been written on it, under the title Let's Go!
But this language is called Go! and not Go, and actually the word "go" is in the public domain: it is the name of a Chinese board game that has existed for thousands of years! (And also a verb in english).
Sites and tools
- The official Go website.
- Go review. A comparison with other C-like languages.