GO结构体类型

来源:互联网 发布:李嘉欣真人知乎 编辑:程序博客网 时间:2024/04/30 02:22

A struct is a sequence of named elements, called fields, each of which has a name and a type. 

Field names may be specified explicitly (IdentifierList) or implicitly (AnonymousField). 

Within a struct, non-blank field names must be unique.


语法:

struct {

IdentifierList Type | AnonymousField

}


e.g.

// An empty struct.
struct {}


// A struct with 6 fields.
struct {
x, y int
u float32
_ float32  // padding
A *[]int
F func()
}


A field declared with a type but no explicit field name is an anonymous field, also called an embedded field or an embedding of the type in the struct.

An embedded type must be specified as a type name T or as a pointer to a non-interface type name *T, and T itself may not be a pointer type. The unqualified type name acts as the field name.


// A struct with four anonymous fields of type T1, *T2, P.T3 and *P.T4
struct {
T1        // field name is T1
*T2       // field name is T2
P.T3      // field name is T3
*P.T4     // field name is T4
x, y int  // field names are x and y
}


The following declaration is illegal because field names must be unique in a struct type:

struct {
T     // conflicts with anonymous field *T and *P.T
*T    // conflicts with anonymous field T and *P.T
*P.T  // conflicts with anonymous field T and *T
}


0 0
原创粉丝点击