I have a globally defined map on a package level. When ever i refer to it, it seems has though the map is recreated on every usage.
1) Is that a correct assumption ?
2) What's the reasoning behind it ? (I'm guessing immutability and thread safety)
3) How would a globally used collection be defined in GO to be used by all packages (i.e. Think of a Global static definition in languages like C#) ?
The package :
package device
import "strconv"
var Devices = map[int]Device { 1 :Device{ name : "Lamp" },
2: Device{ name : "AirConditioner"} }
type Device struct{
name string
Value int
State int
}
func (d *Device) String() string {
return d.name + "," + strconv.Itoa(d.Value) + "," + strconv.Itoa(d.State)
}
Usage :
import d "hello/app/device"
func main() {
device := d.Devices[1]
device.State = 1
// device after change -> Lamp,0,1
fmt.Println("Device After Change -> " + device.String())
deviceAgain := d.Devices[1]
// using the device again prints default values -> Lamp,0,0
fmt.Printf(deviceAgain.String())
}