dotkfcr226217
dotkfcr226217
2017-05-09 10:38
浏览 25
已采纳

通过另一个结构域的类型声明结构域的类型

Here is *s3.GetObjectOutput struct:

type GetObjectOutput struct {
    ...
    Metadata map[string]*string
    ...
}

I want to declare my struct with a struct field has type of Metadata field in GetObjectOutput struct like this

type MyObject struct {
    Metadata *s3.GetObjectOutput.Metadata
    ...
}

But it was not correct. How do I declare a struct with a field has type of another struct's field instead of explicitly write down:

type MyObject struct {
    Metadata map[string]*string
    ...
}

图片转代码服务由CSDN问答提供 功能建议

这里是* s3.GetObjectOutput结构:

  type GetObjectOutput 结构{
 ... 
元数据映射[string] * string 
 ... 
} 
   
 
 

我想用结构声明我的结构 字段在GetObjectOutput结构中具有这样的Metadata字段类型

  type MyObject结构{
元数据* s3.GetObjectOutput.Metadata 
 ... 
} 
   
 
 

但这是不正确的。 如何声明具有字段的结构具有其他结构的字段的类型,而不是显式地记录下来:

  type MyObject struct {
元数据映射[string] * string 
  ... 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanjia4220
    duanjia4220 2017-05-10 00:00
    已采纳

    @William Poussier In that way I have to use a global variable just for use its type. As @zerkms suggested I copied Metadata type from *s3.GetObjectOutput

    type Metadata map[string]*string 
    

    and use:

    type MyObject struct { 
        Metadata
    }
    
    点赞 评论
  • dongwai4434
    dongwai4434 2017-05-09 11:15

    As @zerkms said, you can't. Best idea is to probably create your own field of the same type in MyObject.

    You can also embed the s3.GetObjectOutput in MyObject.

    type MyObject struct {
        *s3.GetObjectOutput
        ...
    }
    

    Given myobj is an instance of MyObject, use myobj.Metadata.

    点赞 评论

相关推荐