[英]golang json marshalling with embedded struct not working
我正在嘗試擴展AWS S3存儲桶類型以包括其他格式並將其封送為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"`
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.