How to assign a value to an uninitialized variable outside of a function

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.


package main

var num int

num  = 5      // syntax error: non-declaration statement outside function body

func main() {

	    // num = 5  // this works at function level


Please see the Go Tour. Variables with initializers are covered in the first chapter :slight_smile:

You can as you say only assign a variable a value in the initialization, or within a function.

“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. :slight_smile:
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.

At the toplevel there is no “later”.

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.


Language Specification

The official Go Language specification.

Which quirk?

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.

1 Like and may be closer to what you are looking to read and may provide some insights into package initialization.

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.

Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.

Anyhoot, thanks Jon for clarifying it’s not explained in the docs anywhere, I’ll stop looking now. And thanks for the link you provided. :slight_smile:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.