我正在尝试扩展 AWS S3 Bucket 类型以包含附加格式并将其编码为 JSON,但编码不会选择附加字段
这是我的
// AWS has this struct already
type Bucket struct {
// Date the bucket was created.
CreationDate *time.Time `type:"timestamp"
timestampFormat:"iso8601"`
// The name of the bucket.
Name *string `type:"string"`
// contains filtered or unexported fields
}
// Extended struct
type AWSS3Bucket struct {
s3.Bucket
location string
}
somefunc()
{
var region string = "us-west-1"
aws_s3_bucket := AWSS3Bucket{Bucket:*bucket, location:region}
jsonString, err := json.Marshal(&aws_s3_bucket)
fmt.Printf("%s\n", jsonString)
}
我得到的只是Bucket的编码。例如,我上面的输出总是这样,没有包含区域
{"CreationDate":"2016-10-17T22:33:14Z","Name":"test-bucket"}
知道为什么该区域没有被编码到 json 缓冲区中吗?
最佳答案
AWSS3Bucket
的 location
字段未导出(即它不是以大写字母开头),因此 json
包不能使用反射找到它。如果导出字段:
type AWSS3Bucket struct {
s3.Bucket
Location string
}
然后它将显示在 jsonString
中。如果您希望它在 JSON 中显示为 "location":...
然后将其标记为:
type AWSS3Bucket struct {
s3.Bucket
Location string `json:"location"`
}
关于json - 带有嵌入式结构的golang json编码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49846968/