</bugz loading .
syntax error: non-declaration statement outside function body
This error usually occurs when a shorthand declaration of a variable ( using := ) is used outside the body of a function. In golang, shorthand declaration of a variable can be only used inside a function body.
An example of the wrong way of declaring a variable outside a function is :
package main
import "fmt"
x := "bugz golang" // cause of the syntax error
func main(){
fmt.Println(x)
}
Other ways of declaring variables can be used instead of the shorthand declaration, if you want to declare variables outside a function ( global variables ).
var x string = "bugz golang"
OR
var x = "bugz golang"
INSTEAD OF
x := "bugz golang"
A mistake in the declaration of a function can also cause this error. For example :
package main
import "fmt"
var x = "bugz golang"
function main(){ // "function" used instead of "func"
fmt.Println(x)
}
In the code above, function is used in the declaration of the main function, instead of the keyword func, which causes the error.
The syntax of the correct way of declaring a function is :
func function_name(){
...
}
In the above code:
func is the keyword which is used to declare functions in golang.
function_name is the name of the function.