英文:
Why can a multiline comment appear between "import" and ImportSpec, but not between PackageName and ImportPath?
问题
这是一个关于导入声明的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.
答案1
得分: 1
以下是翻译好的内容:
一般注释以字符序列/开头,并以第一个后续字符序列/结束。
不包含换行符的一般注释作为空格。其他任何注释作为换行符。
package main
import /*
*/f/*
*/"fmt"
func main() {
fmt.Println()
}
https://go.dev/play/p/nxvIDWkWf_q
prog.go:4:5: 期望为'STRING',发现换行符
英文:
> 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论