为什么golang没有封送json对象? [英] Why golang didn't marshal the json object?
本文介绍了为什么golang没有封送json对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道为什么以下内容无法成功编组json吗?我正在尝试使用一个非常简单的示例来学习json包.
I wonder why the following didn't marshall to json successfully? I am trying to use a very simple example to learn json package.
package main
import (
"encoding/json"
"fmt"
)
type Message struct {
username string `json:"name"`
message string `json:"message"`
}
func main() {
var m = Message{
username: "hello",
message: "world",
}
js, _ := json.Marshal(m)
fmt.Println(m)
fmt.Println(string(js))
}
推荐答案
username
message
以小写字母开头,表示它们未导出(认为是私有的),因此对于 encoding/json
包不可见.您需要导出字段,或实现 MarshalJSON()([] byte,error)
方法并自己完成.
start with a lowercase letter, meaning they are unexported (think private), and so are not visible to the encoding/json
package. You need to export your fields, or implement the MarshalJSON() ([]byte, error)
method and do it yourself.
这篇关于为什么golang没有封送json对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文