I googled and read every online blog on variables and checked all the books I had on Go, and this noobish question strangely isn’t covered anywhere.
How do I assign a value to an uninitialized variable outside of a function ie at the top level in Go? Can you only do it in the initial declaration? I can change it later on in a function, but at the top level I’ve had no luck…seems like it should be an easy step.
eg
package main
var num int
num = 5 // syntax error: non-declaration statement outside function body
func main() {
// num = 5 // this works at function level
println(num)
}
“Variables with initializers
A var declaration can include initializers, one per variable.
If an initializer is present, the type can be omitted; the variable will take the type of the initializer.”
Of course I read that first, that doesn’t explain my question at all, which is initializing a top level variable later on.
I don’t think it’s explained anywhere in the docs. if it is please point me to it.
So, can I not declare a top level variable without a value, and then later on initialize it?
And if so, why isn’t that documented anywhere? Seems like expected behaviour if you come from another language.
Either you intitialize on declaration or in init or any other function call, but you can not declare at line 3 and assign/initialize at line 4 without a surrounding function.
Also, to be honest, I can not see a need for doing such things…
Thanks for the answer, that’s what I’d deduced from my experiments but wanted confirmation from someone experienced with Go…do you know why it’s designed like that? Seeing as you CAN initialize after declaration at function level.
Is that specific behaviour actually documented anywhere, either in the official docs or elsewhere? I searched a long time and found nothing.
Its kind of documented in the languages grammar, as it does not allow such assignments outside of a function.
The reason is simple. If you were able to split those you might forget to initialize. Or one developer might have not seen that you initialized the variable hundred lines later and then initialises it another time. Both on the “toplevel”, and as the toplevel knows no order, which one wins?
Again, why do you want to split it at all? Usually there is no reason to do so.
Do you have a link for where it says that? I’d like to read it and file that info.
I’m just working out the rules of Go, that’s all, I put together a page of every possible variable assignment/reassignment rule and found that quirk, which I couldn’t find explained anywhere.
I’m not really aware of any compiled language that would allow for such a construct, and even if it were, I bet it is covered by a warning and discouraged in the community.
As to the “rule” that says you can’t assign variables outside of their declaration, there isn’t one. Instead there just isn’t a way to do it. Think of it like a car that cannot fly; there is no rule that says “this car cannot fly” but because it doesn’t have wings, a jet engine, etc, it simply cannot fly. Similarly Go just doesn’t have any grammar that allows you to assign package level variables after initialization (outside of an init function or another function).
Well the Tour of Go does explain that the short variable declaration can’t be used outside a function, which is a similar thing, so I’m not sure the car analogy holds up.