gogo-map

golang map not adding element


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"`
}

Solution

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