Why can a multiline comment appear between "import" and ImportSpec, but not between PackageName and ImportPath?

huangapple go评论76阅读模式
英文:

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

以下是翻译好的内容:

Go编程语言规范

注释

一般注释以字符序列/开头,并以第一个后续字符序列/结束。

不包含换行符的一般注释作为空格。其他任何注释作为换行符。

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

huangapple
  • 本文由 发表于 2022年7月8日 15:13:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/72907931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定