I have the following in the interpreter in the tour of go:
package main
import "fmt"
var someString = "one two three four "
var words = strings.Fields(someString)
var length = len(words)
fmt.Println(words, length)
I get
tmp/sandbox216066597/main.go:11: syntax error: non-declaration statement outside function body
I recently corrected it by using var
instead of :=
short syntax outside of any functions, but the error is the same as before.
Your issue isn't with the variable declarations, it's with the fmt.Println line. You must move this inside of a function:
func main() {
fmt.Println(words, length)
}
GoPlay here:
https://play.golang.org/p/JhUnNEIxIY