【发布时间】:2020-09-03 05:02:38
【问题描述】:
要将值是字符串的映射转换为值指向字符串的映射,我需要先复制字符串。如果我不这样做,所有的值都是相同的,可能是错误的值。为什么是这样?我这里没有取字符串文字的地址。
func mapConvert (m map[string]string) map[string]*string {
ret := make(map[string]*string)
for k, v := range m {
v2 := v[:]
ret[k] = &v2
// With the following instead of the last 2 lines,
// the returned map have the same, sometimes wrong value for all keys.
// ret[k]=&v
}
return ret
}
【问题讨论】: