Sobes.tech
Junior — Middle

How can you add an element to a Map that has not yet been created or initialized?

sobes.tech AI

Answer from AI

In Go, before adding an element to a map that has not yet been initialized, you need to create the map itself using the make function. If you try to add an element to a nil map, the program will panic.

Example:

var m map[string]int // m == nil

// Initialize the map
m = make(map[string]int)

// Add an element
m["key"] = 42

Therefore, to add an element to an uninitialized map, first create it using make, and then add elements.