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

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

Syntax Error: Non-declaration statement outside function body

问题

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

package main
import "fmt"
func makeEvenGenerator() func() uint {
    i := uint(0)
    return func() (ret uint) {
        ret = i
        i += 2
        return
    }
}
func main() {
    nextEven := makeEvenGenerator()
    fmt.Println(nextEven()) // 0
    fmt.Println(nextEven()) // 2
    fmt.Println(nextEven()) // 4
}

当我运行它时,我得到错误消息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:

package main 
import "fmt"
func makeEvenGenerator() func() uint {
	i := uint(0)
	return func() (ret uint) {
		ret = i
		i += 2
		return
	}
}func main() {
	nextEven := makeEvenGenerator()
	fmt.Println(nextEven()) // 0
	fmt.Println(nextEven()) // 2
	fmt.Println(nextEven()) // 4
}  

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. 为了允许复杂语句占据一行,可以在闭合的“)”或“}”之前省略分号。

错误在这里,

}func main() {

修改为,

}
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,

}func main() {

Write,

}
func main() {

答案3

得分: 4

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

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

func evenGenerator() <-chan uint {
    ch := make(chan uint)
    go func() {
        i := uint(0)
        for {
            ch <- i
            i += 2
        }
    }()
    return ch
}

func main() {
    evens := evenGenerator()
    for i := 0; i < 3; i++ {
        fmt.Println(<-evens)
    }
}

[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:

func evenGenerator() &lt;-chan uint {
	ch := make(chan uint)
	go func() {
		i := uint(0)
		for {
			ch &lt;- i
			i += 2
		}
	}()
	return ch
}

func main() {
	evens := evenGenerator()
	for i := 0; i &lt; 3; i++ {
		fmt.Println(&lt;-evens)
	}
}

<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:

确定