2

Since the Go lang is getting popular, I started to play with it. I do derive from Java language so when I saw the Go syntax it has been a bit strange to me.

sample code from go lang project site:

func NewField(w, h int) *Field {
    s := make([][]bool, h)
    for i := range s {
        s[i] = make([]bool, w)
    }
    return &Field{s: s, w: w, h: h}
}
  1. Function return type comes after the name, just like in TypeScript.
  2. Additional new variable declaration operator consist of := plus regular = assignment operator.
  3. Reference and pointers usage.
  4. At first sight, strange return statement.
  5. Method names are starting capitalized, ex.: a.Set(rand.Intn(w), rand.Intn(h), true), and so on.

I know that google team based on C syntax. Yet Java is also C/C++ based and at first impression it seems more readable.

Obviously, after some time, we could used to it, yet I would expect from new language to be even more readable than for example Java.

Therefore my question, what was the motivation behind implementation of the syntax in that one of the 'newest' languages?

Why it seems less readable than more mature languages?

closed as primarily opinion-based by gnat, Robert Harvey, David Arno, Doc Brown, Derek Elkins Aug 22 '17 at 2:06

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    := is not assignment, it declares a new variable. Assignment is =, just like in C. – CodesInChaos Aug 21 '17 at 16:29
  • 1
    At a glance I see little difference between pointer syntax in C and in go (except it uses consistent prefix syntax for types, hardly a surprising change, considering how horrible the C syntax is in that area). And the return looks exactly like in java or C too. – CodesInChaos Aug 21 '17 at 16:44
  • I cannot agree that return statement looks exactly the same as in Java, except the return word itself. In Java curly braces are used to initialize an array, eventually. In the sample above its been used to initialize, well, what? I believe some kind of new Field object. Additionally, for sure there are differences in the pointer syntax, etc. Nobody said it is same. Yet this is not the main subject of the question. – DevDio Aug 21 '17 at 16:51
  • 5
    Well, it's based on C syntax, because that's what everyone knows. I wouldn't expect a dramatic increase in readability from C syntax if C syntax is the starting point. If you find it less readable, that's only because you're not familiar with it. – Robert Harvey Aug 21 '17 at 18:02
  • 1
    Syntax readability is mainly familiarity. If you learned Java before Go, it would make sense that the Java syntax is more readable to you. That said, the complexity of a language grammar does indicate the difficulty of learning the grammar. – Frank Hileman Aug 22 '17 at 1:15
5

Going from Java to Go is strange if you expect it to only be different from Java the way C# is different from Java. That is, not at all. Let Python, Lisp, Closure, or, gasp, PERL break your brain before you proclaim Go to be "a bit strange".

I'm going to tackle these in reverse order so I can save the best for last.

5 Method names are starting capitalized, ex.: a.Set(rand.Intn(w), rand.Intn(h), true), and so on.

That's a style from c#. Yawn.

4 At first sight, strange return statement.

That's not a strange Go return statement.

func vals() (int, int) {
    return 3, 7
}

That's a strange Go return statement.

3 Reference and pointers usage.

Oh yeah, I remember those. C++ rides again.

2 Additional new variable declaration operator consist of := plus regular = assignment operator.

They gave us const and we ignored it. We're getting what we deserve.

1 Function return type comes after the name, just like in TypeScript.

Thank god. There's a reason for that.

Any decent user interface designer will tell you the important stuff goes in the upper left corner (so long as you read left to right, top to bottom). It certainly doesn't belong in the middle of a pile of text. So where did we put the function name?

/**
 * Well gee I guess you like it when looking for the name of your function 
 * is a fun visual exercise like where's waldo.
 *
 * @returns what you'd expect something named Pi to return but since 
 * we're "professionals" I thought I'd tell you again because duplicate
 * documentation never hurt anyone. 
 */
public double Pi() {
    double result;
    result = CalculatePiUsingRandom(x->Random.getdouble(x));  
    return result;
}

Compare with:

func pi() double {
   '''Yeah I know triple quote comments are a python thing but you get my point'''
}

I'm in favor of anything that moves the function name back to the upper left corner where it belongs. Sorry if doing what makes sense seems a bit strange.

  • The languages you have mentioned are mature. And yes, Perl can be tricky, nevertheless that is also mature language in comparision to Go. The return statement you have mentioned is indeed not usual (2 vals returned), but it is readable (which I kindly remind you, is the main subject here). – DevDio Aug 22 '17 at 10:31
  • I agree with most of this answer. However, I find a lot of weird inconsistencies in Go syntax. For example, in your vals() function, why does it declare the multiple return types inside required parentheses, while the return statement cannot use parentheses? Why is a trailing comma in a struct required if there is a newline before the closing brace, but prohibited if there is not a newline? (from what I can tell, I'm very beginner) – user949300 Jan 18 at 1:27
  • @candied_orange, Let me know the motivation behind for loop syntax – MASh May 5 at 10:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.