为什么要在“导入”之间出现多行论。和ImportSpec,但不在Packagename和ImportPath之间?

发布于 2025-02-13 22:34:14 字数 430 浏览 1 评论 0原文

这是导入声明的GO规格:

ImportDecl       = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
ImportSpec       = [ "." | PackageName ] ImportPath .
ImportPath       = string_lit .

以下代码编译:

import /*
 */f "fmt"

但不是此代码:

import /*
 */f/*
 */"fmt"

更奇怪的是,此代码编译:

import /*
 */f /* */ "fmt"

我无法理解令牌中这些评论块之间的区别。

This is the Go spec for an import declaration:

ImportDecl       = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) .
ImportSpec       = [ "." | PackageName ] ImportPath .
ImportPath       = string_lit .

The following code compiles:

import /*
 */f "fmt"

But not this code:

import /*
 */f/*
 */"fmt"

Even more strangely, this code compiles:

import /*
 */f /* */ "fmt"

I could not understand the difference between these comment blocks among the tokens.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2025-02-20 22:34:14

GO编程语言规范

注释

常规评论以字符序列 /*开头,然后停止
后续字符序列 */。

一般的评论不包含新线,就像一个空间一样。其他
评论就像新线一样。

package main

import /*
 */f/*
 */"fmt"

func main() {
    fmt.Println()
}

prog.go:4:5: expected 'STRING', found newline

The Go Programming Language Specification

Comments

General comments start with the character sequence /* and stop with
the first subsequent character sequence */.

A general comment containing no newlines acts like a space. Any other
comment acts like a newline.

package main

import /*
 */f/*
 */"fmt"

func main() {
    fmt.Println()
}

https://go.dev/play/p/nxvIDWkWf_q

prog.go:4:5: expected 'STRING', found newline
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文