【发布时间】:2017-04-09 12:24:53
【问题描述】:
我有一个自定义类型 map[string]map[string]string 正在尝试保存在 Google 数据存储中,保存按预期工作。但是加载函数抱怨assignment to entry in nil map
//Address represents address
type Address map[string]map[string]string
上面的类型是map[string]string的映射,目的是保存不同的地址类型。
//Load function from PropertyLoaderInterface helps datastore load this object
func (a *Address) Load(dp []datastore.Property) error {
for _, property := range dp {
(*a)[property.Name] = util.InterfaceToMapString(property.Value)
}
return nil
}
在加载函数中,我引用了作为 map[string]string 映射的地址,它保存了以下示例 JSON 格式。
"Company":{
"physicalAddress": "",
"postalAddress": "",
"physicalCity": "",
"postalCity": "",
"physicalCode": "",
"postalCode": "",
"physicalCountry": "",
"postalCountry": ""
}
下面的保存功能运行良好,数据存储在数据存储中。然而,Load 是一个相当棘手的虫子。
//Save function from PropertyLoaderInterface helps datastore save this object
func (a *Address) Save() ([]datastore.Property, error) {
propertise := []datastore.Property{}
for name, value := range *a {
propertise = append(propertise, datastore.Property{Name: name,
NoIndex: true,
Value: util.MapToJSONString(value)})
}
return propertise, nil
}
地址结构的工作负载
func (a *Address) Load(dp []datastore.Property) error {
*a = make(Address)
for _, property := range dp {
(*a)[property.Name] = util.InterfaceToMapString(property.Value)
}
return nil
}
【问题讨论】:
标签: google-app-engine go google-cloud-datastore