我在 Go 之旅的解释器中有以下内容:
package main
import "fmt"
var someString = "one two three four "
var words = strings.Fields(someString)
var length = len(words)
fmt.Println(words, length)
Run Code Online (Sandbox Code Playgroud)
我明白了
tmp/sandbox216066597/main.go:11: syntax error: non-declaration statement outside function body
Run Code Online (Sandbox Code Playgroud)
我最近通过在任何函数之外使用var而不是:=短语法来纠正它,但错误与以前相同。
您的问题不在于变量声明,而在于 fmt.Println 行。您必须将其移至函数内部:
func main() {
fmt.Println(words, length)
}
Run Code Online (Sandbox Code Playgroud)
在这里玩: https:
//play.golang.org/p/JhUnNEIxIY
| 归档时间: |
|
| 查看次数: |
4058 次 |
| 最近记录: |