4 ms·
Go's small number of keywords is deceptive. Let's look at some of Java's keywords which have no analogue in the Go specification's list of keywords 1. boolean
by TheDong 9y ago
Go's small number of keywords is deceptive.
Let's look at some of Java's keywords which have no analogue in the Go specification's list of keywords
1. boolean: go has the `bool` type, but it's a builtin, not a keyword, so it doesn't count against it.
2. byte: same thing
3. null: Go's `nil` is again not counted as a keyword in their list of 25
....
And hey, that accounts for almost the entire difference if you count all the primitive types.
Go, on the other hand, does not consider those "keywords", which leads to absolutely silly behaviour.
A programmer can accidentally redefine `len` to be a variable, and then `len(x)` will be an error later.
Because go has a ton of important builtins which aren't reserved keywords, you can write incredibly confusing code:
https://play.golang.org/p/-xChCblMEh https://play.golang.org/p/-xChCblMEh
For a nice list, check out https://play.golang.org/p/yjkxqVY4eo https://play.golang.org/p/yjkxqVY4eo
... yeah, Go's small number of keywords is often touted by Gophers, but really it's because the go team made this incredibly stupid decision.
If you counted the same sorts of things as keywords that java did, Go would have almost exactly the same number... and more problems would be caught at compile-time (like `len := 1`) .
- Vendan 9y agoI think the true argument is that you can read the Go specification in a slightly extended sitting, whereas (for example), the current working draft for C++ is 1416 pages.
- kibwen 9y agoThe Go specification is not the same sort of document as the C++ or Java specifications. Go is doubtlessly less extensive than C++ or Java, but it would be dishonest to attempt to leverage their respective specification documents as yardsticks for complexity. If Go were submitted to a standards body such as ISO, the drafting committee would produce a more detailed and more verbose document than currently exists.
- camus2 9y agoIs the Go "specification" considered a normative spec, like ECMASCRIPT spec? And AFAIK the C++ draft also covers the std lib.
- Vendan 9y agoUnless the C++ draft is 90+% stdlib, it's still quite a difference in size (printed, which would not be a good way to use it, the Go spec is 90 pages). And I'd consider it normative, though not to the point of "spec wins if the spec and the toolchain disagree".
- neild 9y agoIf the spec and an implementation disagree, there is a bug in one or the other. (There are multiple implementations of the language.) The Go spec is not entirely normative in that a disagreement between it and the implementation may be resolved in favor of the later. It is normative in that you should be able to implement the entire language from the spec alone.
- didibus 9y agoI'm not sure Go has a spec. The page says spec, but the content says its the reference manual. So I'm guessing it's more focused on learning how to use the language then on writing an alternative implementation of it.
- crawshaw 9y agohttps://golang.org/ref/spec https://golang.org/ref/spec certainly is a specification of the Go language. Whenever a difference is found between the two Go compilers, gccgo and gc, the spec is consulted to see which is right. If the spec is not clear on which compiler is right, then the spec is changed so it is. When the third frontend was written (go/parser and go/types in the standard library), the spec played a similar role. Though far fewer changes were made to the spec, suggesting it is getting pretty good.
- didibus 9y agoOh, that's good to know. I'm happy to hear it is trying to be a language with a formal spec.
- vorg 9y ago> If you counted the same sorts of things as keywords that java did, Go would have almost exactly the same number Java has 53 keywords, whereas Go has 25 keywords and 39 special identifiers, totalling 64, a little more than Java's but less than C#'s 78.