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


(Bevo009) #1

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)
}

(Kristoffer Berdal) #2

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

https://tour.golang.org/basics/9

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


(Bevo009) #3

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


(Norbert Melzer) #4

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…


(Bevo009) #5

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.


(Norbert Melzer) #6

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.


(Bevo009) #7

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.


(Norbert Melzer) #8

From https://golang.org/doc/:

Language Specification

The official Go Language specification.


(Norbert Melzer) #9

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.


(Jon Calhoun) #10

https://golang.org/ref/spec#Package_initialization and https://programming.guide/go/package-init-function-main-execution-order.html 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).


(Bevo009) #11

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.

https://tour.golang.org/basics/10

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:


(system) #12

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