Go 无法将nil转换为x类型
考虑以下示例:Go 无法将nil转换为x类型,go,Go,考虑以下示例: 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.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()
例外情况是按行注释的,我真的不明白我做错了什么
store
是一款map[int]产品
有什么想法吗?您使用的是
store
,就好像它被声明为:
store := make(map[int]*Product)