-1

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

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.