golang json marshalling with embedded struct not working

时间:2018-04-15 21:29:10

标签: json go marshalling

我正在尝试扩展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缓冲区中的任何想法?

1 个答案:

答案 0 :(得分:4)

$(document).ready(function() { $.each($('.autocomplete'), function(i,e) { $(e).autocomplete({ source: "/api/get_key_name/", minLength: 2 }); }); }); location字段未导出(即不以大写字母开头),因此AWSS3Bucket包无法使用反射找到它。如果您导出该字段:

json

然后它会显示在type AWSS3Bucket struct { s3.Bucket Location string } 中。如果您希望它在JSON中显示为jsonString,则将其标记为:

"location":...