英文:
Cannot convert nil to type x
问题
考虑以下示例:
lock.RLock()
var product *Product
if store[productId] != nil { //无法将nil转换为Product类型
product = &Product{}
*product = *store[productId] //store[productId]的间接引用无效(类型为Product)
}
lock.RUnlock()
每行的注释表示异常,我真的不明白我做错了什么。。
store
是一个 map[int]Product
有什么想法吗?
英文:
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?
答案1
得分: 6
你正在将store
用作已声明的方式:
store := make(map[int]*Product)
英文:
You are using store
as if it were declared as:
store := make(map[int]*Product)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论