This Go program will not compile. It throws the error global_var declared and not used
package main
import "log"
var global_var int
func main() {
global_var, new_string := returnTwoVars()
log.Println("new_string: " + new_string)
}
func returnTwoVars() (int, string) {
return 1234, "woohoo"
}
func usesGlobalVar() int {
return global_var * 2
}
However, when I remove the need for using the :=
operator by declaring new_string
in the main function and simply using =
, the compiler doesn't have a problem with seeing that global_var
is declared globally and being used elsewhere in the program. My intuition tells me that it should know that global_var
is declared already