vars in Go packages

Variables in Go packages.

If you’re coming from C++ or PHP you might know the static keyword.
static int myintvar

And since Go’s package structure is pretty weird, I wanted to see how vars behave in packages.
Are they static or are they different depending on who its parent is?

https://github.com/dalu/inheritance

The result is, since a package import is programwide, if another package imports the same package, it imports the same static memory.
In other words, if you set a var to be 10 in package one, import this from main, and import package one from package two, the value of that variable in package one will be 10 for package main and package two.

main <- one main <- two <- one main says one.One = 1 two asks about one.One, gets 1 two sets one.One = 2 main asks about one.One gets 2

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.