by Merkur on 5/11/15, 5:59 AM with 219 comments
# used to set a canonical import path for a package: //import "foo"
# used to generate code: //go:generate bar
# used to document the result of a example function: //Output: foo
Comments should not be directives. Comments are free form, they do not have a syntax (as demonstrated in the examples). Comments are for humans - programms should ignore them or threat them as - comments!
It is my optinion that if Go needs some kind of annotation, than there should be a new syntax for it.
I would propose that directives to the tool chain will be marked with
#TAG: DIRECTIVE
a tool would be able to register the TAG with the go build environment. If a TAG is found in the code - the tool is called to do what ever needs to be done.
by TheDong on 5/11/15, 7:17 AM
In go 1.4, save the following to a file and run 'go generate' on it: https://play.golang.org/p/9WJtxClRXr (I'd make it run in the playground, but you can't 'fork exec', so exec.Command($GOBIN, generate) won't work sadly)
Even though that code has no comments (only a multi-line string), go generate will run and print out a '\'.
I also used that generate command to point out that with generate, as it exists in 1.4, there's actually NO WAY to print out a dollar sign.
The dollar-sign part of this is fixed in master (so go1.5), but it was fixed not by allowing '\$', but by the bizarre $DOLLAR environment variable. See https://go-review.googlesource.com/#/c/8091/
These "features" make the use of comments as directives even worse, because it's NOT comments being used as directives in the case of go generate, but simple string matching. It literally just loops through lines of text and checks if it startswith the correct substring.
This was, of course, done due to lazyness (same as $DOLLAR and many other 'features' of go), and the lazyness is starting to show through the cracks here and there.
Go often prefers pain for the programmer for the compiler developer's life being simpler by about 5 minutes.
by brandonbloom on 5/11/15, 7:51 AM
CGO, for example, expects a comment immediately before a special import statement. A blank line between that comment and the import statement means that the comment is not attached to the import statement's AST node, and so is ignored. This confused me a fair amount when I started with CGO.
Part of the problem with comment syntax is that it's intentionally lexical and uniform. This simplicity means that you can easily obliterate the content of comments at any point in the input file stream via the tokenizer. However, Go instead has to bend over backwards in it's compiler toolchain to preserve comments beyond the lexer: in the parser, AST, code generator, etc.
Compare to docstrings in SmallTalk/Python/Clojure, where they are actual parsed string literals. Also compare to C#, which has explicit and distinct syntax for comments, metadata, and compiler directives. Comments can be inserted more or less anywhere. Metadata only where it's unambiguously attached to an AST node. And directives similarly to statements, unambiguously detached from other AST nodes.
With proper syntax for metadata, the CGO case would have been a compile time error about no AST node for the metadata to be attached to.
With proper syntax for directives, the //go:generate string-scanning bug would have never happened.
These syntaxes must be disjoint from comment syntax to eliminate the problems.
by marcus_holmes on 5/11/15, 7:43 AM
I raised this on the golang dev forums and got nowhere: https://groups.google.com/d/msg/golang-dev/r4rdPdsH1Fg/yjOOz...
The response was basically "we disagree" and I wandered away feeling confused that so many bright people couldn't see the problem here.
by breakingcups on 5/11/15, 8:18 AM
This in itself isn't bad, but the problem it causes is that the Googlers (who do not experience the pain that other users of the language do) are still the majority voice in any decision. Maybe not in numbers, but definitely in weight.
There have been a number of changes pushed through that make sense in Google's use case but not for general development use. Stuff like declaring a folder named "internal" special where it wasn't before. Perfectly understandable in Google's internal uniform codebase, where nearly every package is understood and the world is small. Not a smart change to make a few years into the 1.0 compatibility freeze though, since you can bet that a lot (I even dare say a majority) of 'regular' developers won't know about this and might run into problems because of this at one point or another.
This is just a small example that I can recall out of the top of my head but my worry is that this is happening more often with a lot of features.
Counterarguments from Googlers are usually along the line of "Go has X contributors, only Y of which are Googlers", but that is not the full truth. The top 5 contributors are still employed by Google. If you read the golang-dev mailing list, especially when it comes to discussions about tooling and ecosystem, you'll see that the Googlers have an overwhelming voice and will not shy away from flat-out rejecting criticism to proposals that might be nice for Google's use case but cause ambiguity and complexity for other developers.
Having said that, I still love the language and design. I just whished they shied away from magic comments and half-baked solutions for dependency management.
by aikah on 5/11/15, 6:44 AM
.
by enneff on 5/11/15, 6:47 AM
We made the decision that "//go:" is the syntax for comment directives. The old ones stay around for compatibility reasons.
Previous discussion: https://groups.google.com/forum/#!searchin/golang-dev/commen...
by lohengramm on 5/11/15, 8:32 AM
This is probably the only decision I disagree with the Go team to date.
by fredkbloggs on 5/11/15, 4:50 PM
Note that C compilers already have the attributes people are demanding here: pragma directives are parsed, unknown pragmas generate errors or warnings, they are both visually and syntactically distinct from comments (intended for humans, not the toolchain), etc.
Solved problem. There's no reason to invent new syntax here.
by egeozcan on 5/11/15, 9:46 AM
Look at all the work done on JavaScript to generate code from macros and how incompatible the libraries became (JSX, sweet.js, TypeScript and so on). If there's no standardization, I'm afraid, go may have the same destiny.
If anyone on the Go core team is reading this, I'd like you to consider adding proper macros to the language.
by hartcw on 5/11/15, 9:02 AM
For example for generating a c/c++ enum:
enum example_t
{
#py \
for line in open('values.txt'): \
print('EXAMPLE_' + line.strip() + ',')
EXAMPLE_Max,
EXAMPLE_Unknown
};
Or alternatively to stuff it in a comment: enum example_t
{
/* py
for line in open('values.txt'):
print('EXAMPLE_' + line.strip() + ',')
*/
EXAMPLE_Max,
EXAMPLE_Unknown
};
If anyones interested, heres the python script I use to preprocess the c/c++ files https://github.com/hartcw/cppyby _yosefk on 5/11/15, 12:13 PM
One "benefit" of this is that an older tool not updated to include the new syntax embedded in the comments will remain blissfully unaware of its ignorance, instead of spitting an error message. Another "benefit" is that a programmer unaware of the syntax might delete or copy & paste such comments when doing massive editing, without realizing that they aren't comments. To achieve the latter, it helps if the syntax looks like English or, better yet, if it looks like commented-out code (and, come to think of it, it's not unlikely that it will look like at least one of these things.)
by bane on 5/11/15, 12:10 PM
by copsarebastards on 5/11/15, 4:37 PM
by fixxer on 5/11/15, 1:01 PM
I think this is a semantic argument.
by saintfiends on 5/11/15, 8:02 AM
by nicerobot on 5/11/15, 11:14 AM
by nulltype on 5/11/15, 7:17 AM
by donatj on 5/11/15, 10:42 AM
by jdkanani on 5/11/15, 7:44 AM
I am in favor of #TAG: DIRECTIVE or @TAG:DIRECTIVE
by dvirsky on 5/11/15, 7:56 AM
When PHP did it for Python-style decorators everyone thought it was stupid. And while the build tags in Go are okay by me, doing it for CGO especially seems like a hack.
by scott_s on 5/11/15, 1:49 PM
#!/usr/bin/env python
http://en.wikipedia.org/wiki/Shebang_%28Unix%29by amelius on 5/11/15, 12:52 PM
by erikb on 5/11/15, 9:36 AM
by donlzx on 5/11/15, 11:43 AM
Go's backward compatible promise is hurting itself. By sticking with strict compatibility mode, ad hoc solutions for fundamental issues are added from time to time. Sooner or later, those patch works will back-fire.
For a language so young (version 1.0 in 2012), they should keep compatibility for stable release (1.x), but start adding language features in 2.x branches ASAP.
Yes, compatibility promise may help selling the Go language at the beginning, but Go authors may seem a little too confident/optimistic of the language spec. in this case. If a 2.x branch is not coming out soon enough (in about two year), we will probably face the same dilemma as Python 2 vs 3.
by aablkn on 5/11/15, 9:27 AM
// +build !linux
build constraints feature. It's been there since go 1.0 afaik.by bsaul on 5/11/15, 6:51 AM
They could use the same convention as typescript and use a triple slash instead. That wouldn't change much of the language and impact IDEs, but at least distinguish between human comments and tooling instructions.
by yyhhsj0521 on 5/11/15, 12:43 PM
#--utf8--
indicated the encoding of a source file. In most cases, this won't induce ambiguity, though it's potentially dangerous.
by rjsw on 5/11/15, 9:43 AM
by cjslep on 5/11/15, 12:00 PM
!DEC$ ATTRIBUTES DLLEXPORT :: NameOfSubroutine
Also at least Visual Studio will color those comments differently than normal comments.by anacrolix on 5/20/15, 12:46 PM
by turrini on 5/11/15, 1:01 PM
seems more appropriate.
by khanhussan on 5/12/15, 10:16 AM
by Dewie3 on 5/11/15, 10:42 AM
// COMMENT we have to check that [...]
:)