This question already has an answer here:
I am new to golang and I am trying to create a map of type map[string]interface{}.
But when I try to create a new key when it doesn't exists, I get a runtime error "panic: assignment to entry in nil map". Can anyone tell me what I am doing wrong please?
Go PlayGround: https://play.golang.org/p/vIEE0T11yl
Here is my code:
package main
func main() {
buffer := Buffer{}
buffer.AddRecord("myKey", 12345)
}
type Buffer struct {
records map[string][]interface{}
}
// ProcessRecord adds a message to the buffer.
func (buffer *Buffer) AddRecord(key string, record interface{}) {
_, ok := buffer.records[key]
if !ok {
buffer.records[key] = make([]interface{}, 0)
}
buffer.records[key] = append(buffer.records[key], record)
}
</div>