英文:
The syntax and semantic of the Go compiler runtime
问题
我在go运行时的runtime.c文件中看到了以下函数定义:
void
runtime∕pprof·runtime_cyclesPerSecond(int64 res)
{...}
和
int64
runtime·tickspersecond(void)
{...}
还有很多类似的声明
void runtime·hashinit(void);
在runtime.h中。
我以前没有见过这种C语法(尤其是带斜杠的那种看起来很奇怪)。
这是标准C的一部分还是一些Plan9方言?
英文:
I was looking at the runtime.c file in the go runtime at
/usr/local/go/src/pkg/runtime
and saw the following function definitions:
void
runtime∕pprof·runtime_cyclesPerSecond(int64 res)
{...}
and
int64
runtime·tickspersecond(void)
{...}
and there are a lot of declarations like
void runtime·hashinit(void);
in the runtime.h.
I haven't seen this C syntax before (specially the one with the slash seems odd).
Is this part of std C or some plan9 dialect?
答案1
得分: 6
这是Go语言包路径的特殊内部语法。例如,
runtime∕pprof·runtime_cyclesPerSecond
表示包路径为runtime∕pprof
中的函数runtime_cyclesPerSecond
。
字符'∕
'是Unicode的除号字符,用于分隔路径元素。字符'·
'是Unicode的中点字符,用于分隔包路径和函数。
英文:
It's Go's special internal syntax for Go package paths. For example,
runtime∕pprof·runtime_cyclesPerSecond
is function runtime_cyclesPerSecond
in package path runtime∕pprof
.
The '∕
' character is the Unicode division slash character, which separates path elements. The '·
' character is the Unicode middle dot character, which separates the package path and the function.
答案2
得分: 5
∕
和·
以及其他类似的字符只是某人决定放在函数名中的随机Unicode字符。在C标识符中,与A
或7
一样,晦涩的Unicode字符(**编辑:**列在C99标准的附录D中(PDF的第452-453页);也可以参考这里)是合法的(至少在支持Unicode的编译器中是如此)。
字符∕
看起来像是运算符,但实际上不是(特别是U+2215 ∕
与U+2F /
(除法)非常相似),将其放在函数名中可能会造成困惑,所以我个人建议不要这样做。显然,Go团队中的某个人决定将它们包含在函数名中的原因超过了潜在的困惑。
(**编辑:**需要注意的是,U+2215 ∕
并没有在附录D中明确允许。正如这里所讨论的,这可能是一种扩展。)
英文:
∕
and ·
and friends are merely random Unicode characters that someone decided to put in function names. Obscure Unicode characters (edit: that are listed in Annex D of the C99 standard (pages 452-453 of this PDF); see also here) are just as legal in C identifiers as A
or 7
(in your average Unicode-capable compiler, anyway).
Char| Hex| Octal|Decimal|Windows Alt-code
----+------+------+-------+----------------
∕ |0x2215|021025| 8725| (null)
· | 0xB7| 0267| 183| Alt+0183
Putting characters that look like operators but aren't (U+2215 ∕
, in particular, resembles U+2F /
(division) far too closely) in function names can be a confusing practice, so I would personally advise against it. Obviously someone on the Go team decided that whatever reasons they had for including them in function names outweighed the potential for confusion.
(Edit: It should be noted that U+2215 ∕
isn't expressly permitted by Annex D. As discussed here, this may be an extension.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论