英文:
Go: Can't unmarshall?
问题
package main
import "fmt"
import "encoding/json"
func main() {
m := make(map[string]string)
m["name"] = "Test"
j, _ := json.Marshal(m)
fmt.Println(string(j))
var unmarshalled map[string]string
_ = json.Unmarshal(j, &unmarshalled)
fmt.Println(unmarshalled)
}
unmarshalled
应该被填充上 JSON 数据 j
。
英文:
package main
import "fmt"
import "encoding/json"
func main() {
m := make(map[string]string)
m["name"] = "Test"
j, _ := json.Marshal(m)
fmt.Println(string(j))
var unmarshalled map[string]string
_ = json.Unmarshal(j, unmarshalled)
fmt.Println(unmarshalled)
}
Shouldn't unmarshalled
be filled with the json data j
答案1
得分: 6
不要忽视你的错误
json: Unmarshal(非指针map[string]string)
unmarshaled
需要是一个指针:
err := json.Unmarshal(j, &unmarshalled)
英文:
Don't ignore your errors
json: Unmarshal(non-pointer map[string]string)
unmarshaled
needs to be a pointer:
err := json.Unmarshal(j, &unmarshalled)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论