Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert nil to type x

Tags:

go

Consider the following example:

    lock.RLock()
    var product *Product
    if store[productId] != nil {     //cannot convert nil to type Product
        product = &Product{}
        *product = *store[productId] //invalid indirect of store[productId] (type Product)
    }
    lock.RUnlock()

The exceptions are as commented per line and i don't really get what i am doing wrong..

store is a map[int]Product

any ideas?

like image 288
FPGA Avatar asked Jul 13 '14 03:07

FPGA


1 Answers

You are using store as if it were declared as:

store := make(map[int]*Product)
like image 200
peterSO Avatar answered Oct 19 '22 22:10

peterSO