语法错误:非声明语句在函数体外部

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

Syntax Error: Non-declaration statement outside function body

问题

函数makeEvenGenerator应该返回一个按顺序生成偶数的函数:

  1. package main
  2. import "fmt"
  3. func makeEvenGenerator() func() uint {
  4. i := uint(0)
  5. return func() (ret uint) {
  6. ret = i
  7. i += 2
  8. return
  9. }
  10. }
  11. func main() {
  12. nextEven := makeEvenGenerator()
  13. fmt.Println(nextEven()) // 0
  14. fmt.Println(nextEven()) // 2
  15. fmt.Println(nextEven()) // 4
  16. }

当我运行它时,我得到错误消息syntax error: unexpected func, expecting semicolon or newlineNon-declaration statement outside function body

这段代码直接摘自Caleb Doxsey的《Go语言编程入门》一书。我不确定问题出在哪里。

英文:

The function makeEvenGenerator is supposed to return a function that generates even numbers in a sequential manner:

  1. package main
  2. import "fmt"
  3. func makeEvenGenerator() func() uint {
  4. i := uint(0)
  5. return func() (ret uint) {
  6. ret = i
  7. i += 2
  8. return
  9. }
  10. }func main() {
  11. nextEven := makeEvenGenerator()
  12. fmt.Println(nextEven()) // 0
  13. fmt.Println(nextEven()) // 2
  14. fmt.Println(nextEven()) // 4
  15. }

When I run it, I get the error syntax error: unexpected func, expecting semicolon or newline and Non-declaration statement outside function body.

The code is taken verbatim from An Introduction to Programming in Go by Caleb Doxsey. I am not sure what the problem is.

答案1

得分: 5

你在makeEvenGenerator的末尾的}main中的func之间缺少一个换行符。

我已经修复了这个错误,并将代码发布到了playground

英文:

You are missing a newline between the "}" at the end of the makeEvenGenerator and the "func" in main.

I fixed the error and posted the code to the playground.

答案2

得分: 5

有关分号的规则。

> Go编程语言规范
>
> 分号
>
> 正式的语法在许多产生式中使用分号“;”作为终止符号。Go程序可以使用以下两个规则省略大部分分号:
>
> 1. 当输入被分解为标记时,如果行的最后一个标记是
> 标识符
> 整数、浮点数、虚数、符文或字符串字面量
> 关键字break、continue、fallthrough或return之一
> 运算符和分隔符++、--、)、]或}
> 那么自动在标记流的末尾插入一个分号。
> 2. 为了允许复杂语句占据一行,可以在闭合的“)”或“}”之前省略分号。

错误在这里,

  1. }func main() {

修改为,

  1. }
  2. func main() {
英文:

There are rules about semicolons.

> The Go Programming Language Specification
>
> Semicolons
>
> The formal grammar uses semicolons ";" as terminators in a number of
> productions. Go programs may omit most of these semicolons using the
> following two rules:
>
> 1. When the input is broken into tokens, a semicolon is automatically inserted into the token stream at the end of a non-blank line if the
> line's final token is
> an identifier
> an integer, floating-point, imaginary, rune, or string literal
> one of the keywords break, continue, fallthrough, or return
> one of the operators and delimiters ++, --, ), ], or }
> 2. To allow complex statements to occupy a single line, a semicolon may be omitted before a closing ")" or "}".

The error is here,

  1. }func main() {

Write,

  1. }
  2. func main() {

答案3

得分: 4

你在makeEvenGenerator的末尾的}之间缺少一个换行符,以及func main之前也需要一个换行符。

此外,还有一种替代方法可以使用通道而不是返回一个函数:

  1. func evenGenerator() <-chan uint {
  2. ch := make(chan uint)
  3. go func() {
  4. i := uint(0)
  5. for {
  6. ch <- i
  7. i += 2
  8. }
  9. }()
  10. return ch
  11. }
  12. func main() {
  13. evens := evenGenerator()
  14. for i := 0; i < 3; i++ {
  15. fmt.Println(<-evens)
  16. }
  17. }
  18. [playground](http://play.golang.org/p/1HklcNfI-w)
英文:

You were missing a new line between } at the end of makeEvenGenerator and func main.

Also an alternative approach for that pattern is to use a channel not return a function:

  1. func evenGenerator() &lt;-chan uint {
  2. ch := make(chan uint)
  3. go func() {
  4. i := uint(0)
  5. for {
  6. ch &lt;- i
  7. i += 2
  8. }
  9. }()
  10. return ch
  11. }
  12. func main() {
  13. evens := evenGenerator()
  14. for i := 0; i &lt; 3; i++ {
  15. fmt.Println(&lt;-evens)
  16. }
  17. }

<kbd>playground</kbd>

huangapple
  • 本文由 发表于 2014年10月11日 04:53:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/26307865.html
匿名

发表评论

匿名网友

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

确定