鍍金池/ 問答/PHP  GO  Linux  HTML/ 關(guān)于golang中的結(jié)構(gòu)體

關(guān)于golang中的結(jié)構(gòu)體

這些天看aws的golang sdk中結(jié)構(gòu)體中有

_ struct{} `type:"structure"`

不知道 下劃線 _ 是什么意思,注意不是tag

// Please also see https://docs.aws.amazon.com/goto/WebAPI/s3-2006-03-01/MultipartUpload
type MultipartUpload struct {
    _ struct{} `type:"structure"`

    // Date and time at which the multipart upload was initiated.
    Initiated *time.Time `type:"timestamp" timestampFormat:"iso8601"`

    // Identifies who initiated the multipart upload.
    Initiator *Initiator `type:"structure"`

    // Key of the object for which the multipart upload was initiated.
    Key *string `min:"1" type:"string"`

    Owner *Owner `type:"structure"`

    // The class of storage used to store the object.
    StorageClass *string `type:"string" enum:"StorageClass"`

    // Upload ID that identifies the multipart upload.
    UploadId *string `type:"string"`
}
回答
編輯回答
不討囍

相當(dāng)于給struct中的字段添加了tag屬性,可以通過reflect包來(lái)獲取struct中字段的屬性

2017年9月5日 23:13
編輯回答
熟稔

這個(gè)叫struct tag

Go offers struct tags which are discoverable via reflection. These enjoy a wide range of use in the standard library in the JSON/XML and other encoding packages.

The community welcomed them and has built ORMs, further encodings, flag parsers and much more around them since, especially for these tasks, single-sourcing is beneficial for data structures.

2017年8月10日 07:54