Categories
My Go Tutorial

Declaring a variable (Part 2

MUST be declared before using it

Order of declaring a variable in your code, here is an example of the declared variable unrecognized by fmt.Println() statement. The GO compiler (go build) took each line (reading from top to bottom) of the source code. In this case, you have a variable name odometer use but not data type and memory address defined. The compilation has failed or IDE for GO may prompt it as a problem.

// Variable declared but return error message
// when compile (go build) 
// 
func main() {
     fmt.Println(odometer)
     var odometer int
}

// Corrected code compile without error and 
// execution of code will return a value 
func main() {
     var odometer int
     fmt.Println(odometer)
}