Reputation: 3964
There is battleFoundmap in my code and i tried the add a element like this:(battle is not nil)
battleFoundMap[battle.ID] = battle.Answers
But when i debug it it returns 1:27: expected '==', found '=' error and not put in it. How to solve it?
Here is map and Card struct
var battleFoundMap map[uint][]models.Card
type Card struct {
gorm.Model
UserId uint `json:"userid"`
Name string `json:"name"`
Meaning string `json:"meaning"`
}
Upvotes: -1
Views: 2432
Reputation: 13476
Adding to @ShivaKishore's answer,
When you declare a map like, var name map[KeyType]ValueType
. The value of this map is nil
and has a length of 0
.
A nil
map has no key-values nor can be added. It behaves like an empty map for read-operations but causes a runtime panic if you want to write data to it.
var m map[string]string
// reading
m["foo"] == "" // works.
// writing
m["foo"] = "bar" // will panic.
But, initializing a map with make
creates an empty map that works with both read and write operations.
// as you can't declare a map globally using shorthands
var m map[string]string
m = make(map[string]string)
or, using shorthands
m := make(map[string]string)
Upvotes: 2
Reputation: 1701
You should initialise a map with make before using it.
change
var battleFoundMap map[uint][]models.Card
to
battleFoundMap := make(map[uint][]models.Card)
That should be enough.
Upvotes: 1