5 ms·
Come on, those are the two weirdest things about go syntax, especially for new comers. (Honestly, why can you not 'for var x = 0; ...'? and only 'for var X :=
by shadowmint 13y ago
Come on, those are the two weirdest things about go syntax, especially for new comers.
(Honestly, why can you not 'for var x = 0; ...'? and only 'for var X := 0; ...; ...' but immediately below var x = 0; is just as valid. The := / var syntax in go is just plain weird).
Go is also the only place I've ever seen this syntax:
if blah {
..
} else {
..
}
Maybe that's popular in some obscure places, but it is weird they made that the only valid syntax.
I mean, fair enough, Go has its own syntax, but these are pretty valid gripes.
- masklinn 13y ago> Go is also the only place I've ever seen this syntax: Do you mean if blah { code } else { code } as in parens-less if statement? Because I don't see anything odd in what you wrote, it's valid C or Java. And if it's putting else, } and { on the same line, it's "standard" K&R, 1TBS, BSD KNF and java indent style/coding conventions.
- jmduke 13y agoI believe he's referring to the latter -- as opposed to: if blah { code } else { code } Personally, I don't prefer the K&R way but I don't think it's either strange nor troublesome enough to worry about for more than a few seconds.
- samatman 13y agoThe argument is that putting anything between } and else breaks the else statement, so using } else { stresses that you cannot add statements either before or after the else.
- shadowmint 13y agoit is weird they made that the only valid syntax
- masklinn 13y agoWhat you stated is not just that (which is not actually weird), but — quoting you > Go is also the only place I've ever seen this syntax compounded by > Maybe that's popular in some obscure places (thus calling Java's coding conventions or the K&R "obscure places")
- doktrin 13y agoI don't understand why this comment is being downvoted. I just learned something. If Go enforces a specific coding convention at the compiler level, then that could legitimately be considered at least slightly unusual. While it's true that this style is far from esoteric, it's also typically just that ( a coding style ). This strikes me as at least somewhat analogous to Python's whitespace indentation rules or Java's checked exceptions, in that what is typically a design choice is enforced by the compiler / interpreter.
- CatMtKing 13y agoIt's a great choice for readability; cuts down on inconsistency.
- wonderzombie 13y agoIt's because of arguments like this, that's why. They render a huge swath of subjective, unproductive discussions about style completely moot.
- jlgreco 13y agoWell, that is the idea at least. Unfortunately it almost seems as though the style arguments will be replaced with the "pro/against enforced style" arguments. Hopefully this new topic has less staying power.
- coldtea 13y agoNot, it's very pragmatic, based on a parser need (to make it simpler), and it's well explained in their docs. Not to mention that's a well known style, has a lot of benefits (less wasted lines compared to the style that puts both brackets on empty lines, less potential for errors in than when you omit the brackets for a single statement), and, most importantly, puts and end to all bikeshedding. Especially along with gofmt, go is a bikeshedding free language in regards to syntax.
- threedaymonk 13y agoThat if/else syntax is used in K&R's The C Programming Language, the source of Unix (and thus of FreeBSD etc.), and it's pretty common in JavaScript. It may not be the most common syntax in modern C, but it's hardly weird or obscure.
- masklinn 13y ago> That if/else syntax is used in K&R's The C Programming Language, the source of Unix (and thus of FreeBSD etc.) And it's part of Sun/Oracle's official java coding conventions.
- codygman 13y agoBecause within the scope of that for loop x has been declared.
- jgrahamc 13y agoCome on, those are the two weirdest things about go syntax, especially for new comers. I don't really understand your 'come on'. I wasn't disagreeing with the OP about point #1, I was merely correcting what he said to make it clearer and I even agree with you (see my note about 'silly) that where you can and cannot use var and := is odd and unclear. As for the concern about Go syntax and brace positioning, it will really depend where you come from. If have any experience of any note in any C-derived language (such as C++, Java, JavaScript, etc.) you will have seen people writing code like that for years.
- _ak 13y ago> Honestly, why can you not 'for var x = 0; ...'? and only 'for var X := 0; ...; ...' but immediately below var x = 0; is just as valid. The := / var syntax in go is just plain weird Because var and := declaration+initialization are semantically different: when declaring multiple, variables, := allows redeclaration (which just overwrites an already declared variable's value instead of shadowing it), while var does not. > Maybe that's popular in some obscure places Err, K&R style (from which Go's style is derived and virtually identical, except for braces of functions) isn't exactly obscure... even the Java's Coding Convention (from 1999) does it exactly like that: http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-142311.html#449 http://www.oracle.com/technetwork/java/javase/documentation/...
- kunai 13y agoI use that conditional syntax all the time with Java. K&R had it back in the 70s.
- wting 13y agoif blah { .. } else { .. } > Maybe that's popular in some obscure places, but it is weird they made that the only valid syntax. That is valid K&R syntax[0], which is used prevalently in C (e.g. Linux kernel). FYI, Rob Pike wrote a few books with Brian Kernighan (the K in K&R). Go is designed for consistency and optimized for the compiler (e.g. mandatory braces, no unused import statements). Declaring a single canonical format (e.g. gofmt / PEP 8) solves a lot of syntax bikeshedding. [0]: https://en.wikipedia.org/wiki/Indent_style#K.26R_style https://en.wikipedia.org/wiki/Indent_style#K.26R_style
- kyrias 13y agoPEP-8 isn't a "canonical format" though and people should stop treating it as one.
- jlgreco 13y agoIt should be. New languages should take a hard, firm but fair, stance on style when they are first created. It is the only opportunity they will get. You can always loosen it later if you really feel the need, but you can't tighten it later on (evidenced by attitudes exactly like yours.) I mean, unless you like endless style flamewars and bickering... Even ignoring that benefit, taking a stance on style like Go has done allows style to be handled mechanically, which is more in line with the general hacker attitude anyway. Offload that stuff to the computer; let the programmer concentrate on doing the things the computer can't.
- pekk 13y agoNo, it pretty much is a canonical format. You don't have to use it, but then you are out of sync with members of the Python community who wish to adhere to the standard format
- coldtea 13y ago>Go is also the only place I've ever seen this syntax Then you should go read some more code. Because not only it's the one I've been using for ages (hah), but it's standard K&R style, one of the oldest and most established styles out there...