英文:
Capitals in struct fields
问题
我正在使用这个库来访问CouchDB(具体来说是Cloudant)"github.com/mikebell-org/go-couchdb",我注意到了一个问题。
当我尝试将一个结构体作为文件添加到数据库时,只有以大写字母开头的字段才会被添加。
例如:
type Person struct {
name string
Age int
}
func main() {
db, _ := couchdb.Database(host, database, username, password)
joe := Person{
name: "mike",
Age: 190,
}
m, _ := db.PostDocument(joe)
}
在这种情况下,只有"Age"字段被更新并插入到我的数据库中。
我还注意到在另一种情况下也存在这个问题 - 当我做类似这样的操作时:
type Sample struct {
Name string
age int
}
joe := Sample{
Name: "xx",
age: 23,
}
byt, _ := json.Marshal(joe)
post_data := strings.NewReader(string(byt))
fmt.Println(post_data)
在这种情况下,只有"Name"会被打印出来:
输出:&{{"Name":"xx"} 0 -1}
为什么会这样?如果我想要一个小写字母开头的字段并且它能够保存在数据库中,是否有可能实现?
英文:
I'm using this library to access CouchDB (cloudant to be specific) "github.com/mikebell-org/go-couchdb" and I've noticed a problem.
When I go to add a file to the database and pass in a struct, only the fields of the struct which started with a capital letter get added.
For example
type Person struct {
name string
Age int
}
func main() {
db, _ := couchdb.Database(host, database, username, password)
joe := Person{
name: "mike",
Age: 190,
}
m, _ := db.PostDocument(joe)
}
In this case, only the "age" field got updated and inserted into my database.
I've noticed this problem in another case also - when I'm doing something like this :
type Sample struct {
Name string
age int
}
joe := Sample{
Name: "xx",
age: 23,
}
byt, _ := json.Marshal(joe)
post_data := strings.NewReader(string(byt))
fmt.Println(post_data)
in this case, only Name would be printed out :
output : &{{"Name":"xx"} 0 -1}
Why is this? and If I would like to have a field with a lowercase and be inside the database, is that possible?
答案1
得分: 85
这是因为只有以大写字母开头的字段才会被导出,换句话说,只有这些字段才能在当前包之外可见(在这种情况下是json
包)。
这是规范中涉及到这一点的部分:http://golang.org/ref/spec#Exported_identifiers
不过,你可以使用所谓的“标签”来反序列化不以大写字母开头的json字段。使用json
包时,可以使用以下语法:
type Sample struct {
Name string `json:"name"`
Age int `json:"age"`
}
请参考文档以获取更多关于此的信息。
英文:
This is because only fields starting with a capital letter are exported, or in other words visible outside the curent package (and in the json
package in this case).
Here is the part of the specifications refering to this: http://golang.org/ref/spec#Exported_identifiers
Still, you can unmarshall json fields that do no start with a capital letters using what is called "tags". With the json
package, this is the syntax to use:
type Sample struct {
Name string `json:"name"`
Age int `json:"age"`
}
Refer to the documentation for more information about this.
答案2
得分: 7
只有以大写字母开头的字段才会被json包转换为字符串。
请参阅http://golang.org/pkg/encoding/json/。
结构体值被编码为JSON对象。每个导出的结构体字段都成为对象的成员,使用字段名作为对象键,除非由于以下原因之一而省略该字段。
您需要像这样定义结构体:
type Sample struct{
Name string `json:"name"`
Age int `json:"age"`
}
英文:
json package only stringfiy fields start with capital letter.
see http://golang.org/pkg/encoding/json/
> Struct values encode as JSON objects. Each exported struct field becomes a member of the object, using the field name as the object key, unless the field is omitted for one of the reasons given below.
You need define the struct like this:
type Sample struct{
Name string `json:"name"`
Age int `json:"age"`
}
答案3
得分: 0
json.Marshal方法中的结构体字段只接受以大写字母开头的字段。
json包只能访问结构体类型的导出字段(以大写字母开头的字段)。因此,只有结构体的导出字段会出现在JSON输出中。
type Sample struct {
Name string
Age int
}
英文:
json.Marshal method struct-in field-i only accepts fields that start with a capital letter
The json package only accesses the exported fields of struct types (those that begin with an uppercase letter). Therefore only the exported fields of a struct will be present in the JSON output.
type Sample struct {
Name string
Age int
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论