TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Go Lang: Comments Are Not Directives

279 点作者 Merkur大约 10 年前
Go 1.4 starts to use comments as directives. I think that is a realy bad path to go on the long run. You see its beginnings in following 3 examples:<p># used to set a canonical import path for a package: &#x2F;&#x2F;import &quot;foo&quot;<p># used to generate code: &#x2F;&#x2F;go:generate bar<p># used to document the result of a example function: &#x2F;&#x2F;Output: foo<p>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!<p>It is my optinion that if Go needs some kind of annotation, than there should be a new syntax for it.<p>I would propose that directives to the tool chain will be marked with<p>#TAG: DIRECTIVE<p>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.

33 条评论

TheDong大约 10 年前
Want to know what&#x27;s even worse? Using comments for something <i>but not parsing the syntax tree</i>.<p>In go 1.4, save the following to a file and run &#x27;go generate&#x27; on it: <a href="https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;9WJtxClRXr" rel="nofollow">https:&#x2F;&#x2F;play.golang.org&#x2F;p&#x2F;9WJtxClRXr</a> (I&#x27;d make it run in the playground, but you can&#x27;t &#x27;fork exec&#x27;, so exec.Command($GOBIN, generate) won&#x27;t work sadly)<p>Even though that code has no comments (only a multi-line string), go generate will run and print out a &#x27;\&#x27;.<p>I also used that generate command to point out that with generate, as it exists in 1.4, there&#x27;s actually NO WAY to print out a dollar sign.<p>The dollar-sign part of this is fixed in master (so go1.5), but it was fixed not by allowing &#x27;\$&#x27;, but by the bizarre $DOLLAR environment variable. See <a href="https:&#x2F;&#x2F;go-review.googlesource.com&#x2F;#&#x2F;c&#x2F;8091&#x2F;" rel="nofollow">https:&#x2F;&#x2F;go-review.googlesource.com&#x2F;#&#x2F;c&#x2F;8091&#x2F;</a><p>These &quot;features&quot; make the use of comments as directives even worse, because it&#x27;s <i>NOT</i> 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.<p>This was, of course, done due to lazyness (same as $DOLLAR and many other &#x27;features&#x27; of go), and the lazyness is starting to show through the cracks here and there.<p>Go often prefers pain for the programmer for the compiler developer&#x27;s life being simpler by about 5 minutes.
评论 #9523229 未加载
评论 #9524338 未加载
评论 #9523295 未加载
评论 #9530120 未加载
brandonbloom大约 10 年前
Comments are also not metadata!<p>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&#x27;s AST node, and so is ignored. This confused me a fair amount when I started with CGO.<p>Part of the problem with comment syntax is that it&#x27;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&#x27;s compiler toolchain to preserve comments beyond the lexer: in the parser, AST, code generator, etc.<p>Compare to docstrings in SmallTalk&#x2F;Python&#x2F;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&#x27;s unambiguously attached to an AST node. And directives similarly to statements, unambiguously detached from other AST nodes.<p>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.<p>With proper syntax for directives, the &#x2F;&#x2F;go:generate string-scanning bug would have never happened.<p>These syntaxes must be disjoint from comment syntax to eliminate the problems.
评论 #9525952 未加载
评论 #9523382 未加载
marcus_holmes大约 10 年前
At last! I&#x27;m not insane!<p>I raised this on the golang dev forums and got nowhere: <a href="https:&#x2F;&#x2F;groups.google.com&#x2F;d&#x2F;msg&#x2F;golang-dev&#x2F;r4rdPdsH1Fg&#x2F;yjOOzqIqNPMJ" rel="nofollow">https:&#x2F;&#x2F;groups.google.com&#x2F;d&#x2F;msg&#x2F;golang-dev&#x2F;r4rdPdsH1Fg&#x2F;yjOOz...</a><p>The response was basically &quot;we disagree&quot; and I wandered away feeling confused that so many bright people couldn&#x27;t see the problem here.
评论 #9524229 未加载
评论 #9523341 未加载
评论 #9523354 未加载
评论 #9526772 未加载
评论 #9523700 未加载
评论 #9525157 未加载
breakingcups大约 10 年前
The problem with Go is that, while the language and compiler are excellent, Google doesn&#x27;t a lot care about tooling &amp; ecosystem. Why? They have their own in-house tooling (which is of course both highly specific to Google&#x27;s use case and closed of for the public).<p>This in itself isn&#x27;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.<p>There have been a number of changes pushed through that make sense in Google&#x27;s use case but not for general development use. Stuff like declaring a folder named &quot;internal&quot; special where it wasn&#x27;t before. Perfectly understandable in Google&#x27;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 &#x27;regular&#x27; developers won&#x27;t know about this and might run into problems because of this at one point or another.<p>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.<p>Counterarguments from Googlers are usually along the line of &quot;Go has X contributors, only Y of which are Googlers&quot;, 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&#x27;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&#x27;s use case but cause ambiguity and complexity for other developers.<p>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.
评论 #9524660 未加载
评论 #9523497 未加载
评论 #9523636 未加载
评论 #9525228 未加载
评论 #9523587 未加载
aikah大约 10 年前
I 100% agree. You want macros? I don&#x27;t like it but think about proper macros. Go generate is just sweeping issues under the carpet and say to developers &quot;it has been dealt with&quot;. Nobody expects a language to be perfect, but sometimes I feel the Go team takes devs for idiots with half baked solutions. So don&#x27;t use comments for anything but documentation generation purpose.<p>.
评论 #9523115 未加载
enneff大约 10 年前
(You left out &quot;&#x2F;&#x2F; +build&quot;, which predates Go 1.4.)<p>We made the decision that &quot;&#x2F;&#x2F;go:&quot; is the syntax for comment directives. The old ones stay around for compatibility reasons.<p>Previous discussion: <a href="https:&#x2F;&#x2F;groups.google.com&#x2F;forum&#x2F;#!searchin&#x2F;golang-dev&#x2F;comments&#x2F;golang-dev&#x2F;r4rdPdsH1Fg&#x2F;yjOOzqIqNPMJ" rel="nofollow">https:&#x2F;&#x2F;groups.google.com&#x2F;forum&#x2F;#!searchin&#x2F;golang-dev&#x2F;commen...</a>
评论 #9523990 未加载
评论 #9523193 未加载
评论 #9523335 未加载
评论 #9523204 未加载
评论 #9523292 未加载
lohengramm大约 10 年前
I also do not agree with that. This is bad for so many reasons. First, to the compiler code, comments should be discarded as soon as possible (often in the lexer). To the programmers, comments should never execute anything (this is just intuitively expected). To any go source code reader (human or machine), comments are supposed to contain only human readable text, probably a context sensitive explanation of that particular section of code. To the separation of concerns, comments are comments and compiler directives are compiler directives. To each his own.<p>This is probably the only decision I disagree with the Go team to date.
评论 #9524160 未加载
评论 #9523372 未加载
fredkbloggs大约 10 年前
The basic syntax of Go is C-like (this is not intended to be controversial), including that of comments. There is also a C syntax for communicating with the compiler: #pragma. There is no need for yet another syntax here. Just follow the example set by the language that has already heavily influenced the Go syntax.<p>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.<p>Solved problem. There&#x27;s no reason to invent new syntax here.
egeozcan大约 10 年前
To this day, I still couldn&#x27;t figure out why Go stays at the edge of being a great language, but lacks few very essential features for -to me- inexplicable reasons.<p>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&#x27;s no standardization, I&#x27;m afraid, go may have the same destiny.<p>If anyone on the Go core team is reading this, I&#x27;d like you to consider adding proper macros to the language.
评论 #9526740 未加载
hartcw大约 10 年前
Although I don&#x27;t use go (yet), I can see the benefit of this mechanism as I use a similar one for my own c&#x2F;c++ code. There I use specially tagged comments to embed python in the c&#x2F;c++ sources, and then run all the code through a python interpret stage to expand&#x2F;execute the python code before compiling it. Actually it supports C macro style for the embedded code, which is a bit more sensible than misusing code comments.<p>For example for generating a c&#x2F;c++ enum:<p><pre><code> enum example_t { #py \ for line in open(&#x27;values.txt&#x27;): \ print(&#x27;EXAMPLE_&#x27; + line.strip() + &#x27;,&#x27;) EXAMPLE_Max, EXAMPLE_Unknown }; </code></pre> Or alternatively to stuff it in a comment:<p><pre><code> enum example_t { &#x2F;* py for line in open(&#x27;values.txt&#x27;): print(&#x27;EXAMPLE_&#x27; + line.strip() + &#x27;,&#x27;) *&#x2F; EXAMPLE_Max, EXAMPLE_Unknown }; </code></pre> If anyones interested, heres the python script I use to preprocess the c&#x2F;c++ files <a href="https:&#x2F;&#x2F;github.com&#x2F;hartcw&#x2F;cppy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hartcw&#x2F;cppy</a>
评论 #9523559 未加载
评论 #9526449 未加载
评论 #9531761 未加载
评论 #9530730 未加载
_yosefk大约 10 年前
One language where this is currently practiced is Verilog. To make things spicier, different vendors have different syntax for semantics-changing comments, often prefixed with the vendor&#x27;s name.<p>One &quot;benefit&quot; 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 &quot;benefit&quot; is that a programmer unaware of the syntax might delete or copy &amp; paste such comments when doing massive editing, without realizing that they aren&#x27;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&#x27;s not unlikely that it will look like at least one of these things.)
bane大约 10 年前
As soon as you start using comments for something other than comments or documentation, you&#x27;ve basically admitted the language is broken and you end up becoming Java.
评论 #9525274 未加载
评论 #9524625 未加载
copsarebastards大约 10 年前
What&#x27;s the point of having a single-pass compiler if you have to add hacky precompilation steps to make up for its shortcomings? If you&#x27;re passing over the code multiple times anyway then it defeats the purpose.
fixxer大约 10 年前
Meh. I hear your gripe, but I think your proposed solution is not that much of an improvement. You&#x27;re also basing all of this on the idea that comments are &quot;free form&quot;. Where is that gospel? I like the idea that comments are more than just blabber. I like the idea that, when structured some way, they take on new meanings.<p>I think this is a semantic argument.
评论 #9524743 未加载
saintfiends大约 10 年前
Partially agreed. Directives should be visually distinct from comments and syntax should be formally defined such that compilers, code generators, editors and IDE&#x27;s can use them consistently.
评论 #9523666 未加载
nicerobot大约 10 年前
Just because _you_ are locked into a mentality about a meaning for &#x2F;&#x2F; does not mean we all are. Explain to me why formatted&#x2F;structured text can not appear after a &#x2F;&#x2F; in a file. If your editor parsed the text and formatted it to look meaningful, like code, would that help?
评论 #9524025 未加载
评论 #9523963 未加载
nulltype大约 10 年前
I don&#x27;t really agree. I don&#x27;t see any substantial difference between &#x2F;&#x2F;go: and #go: except that programs using the # syntax would not build on older versions of go. I haven&#x27;t really run across any issues with the current syntax. What kind of issues are you having?
评论 #9523234 未加载
评论 #9523258 未加载
donatj大约 10 年前
I honestly even feel weird about godoc just being standard comments, in my mind docs and comments are different things, docs usually being noted with &#x2F; * * (no spaces, but can&#x27;t figure out how to escape) and comments being &#x2F;* &#x2F;&#x2F; or #
jdkanani大约 10 年前
I couldn&#x27;t agree more. It also makes code hard to read. I have to look closely to check if that&#x27;s a comment or directive. It would be problem when your code grows.<p>I am in favor of #TAG: DIRECTIVE or @TAG:DIRECTIVE
dvirsky大约 10 年前
This pattern predates Go 1.4, we&#x27;ve had it with CGO and build tags since at least 1.0 AFAIK. Even the Output in examples is pretty old I think. The only advantage in it is backwards compatibility with older Go versions, but in general I agree with you.<p>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.
评论 #9523485 未加载
scott_s大约 10 年前
I don&#x27;t know enough of the matter to have an informed view, but I think it&#x27;s worth noting that there is precedent for this sort of thing. Take from one of my Python scripts:<p><pre><code> #!&#x2F;usr&#x2F;bin&#x2F;env python </code></pre> <a href="http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Shebang_%28Unix%29" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Shebang_%28Unix%29</a>
评论 #9524873 未加载
amelius大约 10 年前
To be fair, it all started with #!&#x2F;bin&#x2F;sh
erikb大约 10 年前
What does the word directive mean? Doing stuff by parsing comments is called a macro processor (or preprocessor, but it doesn&#x27;t have to be done before the actual processing) or not? I think there are many instances where macro processors are a good thing. Some languages have them in the core. So what&#x27;s the difference between macros and directives?
评论 #9523687 未加载
donlzx大约 10 年前
Comments are just comments, we should not keep adding keywords and syntax to them.<p>Go&#x27;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.<p>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.<p>Yes, compatibility promise may help selling the Go language at the beginning, but Go authors may seem a little too confident&#x2F;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.
评论 #9524075 未加载
aablkn大约 10 年前
Oh don&#x27;t forget the good old<p><pre><code> &#x2F;&#x2F; +build !linux </code></pre> build constraints feature. It&#x27;s been there since go 1.0 afaik.
bsaul大约 10 年前
Agree as well.<p>They could use the same convention as typescript and use a triple slash instead. That wouldn&#x27;t change much of the language and impact IDEs, but at least distinguish between human comments and tooling instructions.
评论 #9523119 未加载
yyhhsj0521大约 10 年前
In Python, following comment<p>#-<i>-utf8-</i>-<p>indicated the encoding of a source file. In most cases, this won&#x27;t induce ambiguity, though it&#x27;s potentially dangerous.
评论 #9524701 未加载
评论 #9525332 未加载
rjsw大约 10 年前
Lisp has used directives in comments in the past. Only on the first line of a file but they were parsed.
评论 #9523839 未加载
cjslep大约 10 年前
Sad to see Go is fully going the way of Intel&#x27;s Fortran compiler. It was more reasonable when (I think in cgo) you needed to export functions, which is done in Intel-compiled Fortran like so:<p><pre><code> !DEC$ ATTRIBUTES DLLEXPORT :: NameOfSubroutine </code></pre> Also at least Visual Studio will color those comments differently than normal comments.
anacrolix将近 10 年前
I think the same can be said of the . selector on packages.
turrini大约 10 年前
&#x2F;&#x2F;go:fancy<p>seems more appropriate.
khanhussan大约 10 年前
:p -_- very nice
Dewie3大约 10 年前
If you&#x27;re feeling paranoid and don&#x27;t have time to look up the set of magical incantations, maybe you should write your actual comments like this:<p><pre><code> &#x2F;&#x2F; COMMENT we have to check that [...] </code></pre> :)
评论 #9524368 未加载