前言

反射
img

我们通过两个问题来解决他的疑惑:

  1. 结构体在内存中是如何存储的

  2. 反射获取结构体成员信息的过程

结构体是如何存储的

结构体是占用一块连续的内存,一个结构体变量的大小是由结构体中的字段决定的,结构体变量的地址等于结构体第一个字段的首地址。示例:

type User struct {
 Name string
 Age uint64
 Gender bool // true:男 false: 女
}

func main(){
 u := User{
   Name: "asong",
   Age: 18,
   Gender: false,
  }
 fmt.Printf("%p\n",&u)
 fmt.Printf("%p\n",&u.Name)
}
// 运行结果
0xc00000c060
0xc00000c060
uName

结构体的内存布局其实就是分配一段连续的内存,具体是在栈上分配还是堆上分配取决于编译器的逃逸分析,结构体在内存分配时还要考虑到内存对齐。

CPUword size32CPU4CPU4CPUCPU42
CGoCGo
  • 对于结构的各个成员,第一个成员位于偏移为0的位置,结构体第一个成员的偏移量(offset)**为0,以后每个成员相对于结构体首地址的 offset 都是**该成员大小与有效对齐值中较小那个的整数倍,如有需要编译器会在成员之间加上填充字节。

  • 除了结构成员需要对齐,结构本身也需要对齐,结构的长度必须是编译器默认的对齐长度和成员中最长类型中最小的数据大小的倍数对齐。

Usermac64CPU8Stringuint64bool881
string816uin64888Name16bool111User2424

接下来我们在分析第二个规则:

25281682583270
img

注意:这里对内存对齐没有说的很细,想要更深了解内存对齐可以看我之前的一篇文章:Go看源码必会知识之unsafe包

Go语言反射获取结构体成员信息

Go语言提供了一种机制在运行时更新和检查变量的值、调用变量的方法和变量的内在操作,但是在编译时并不知道这些变量的具体类型,这种机制被称为反射。Go语言提供了 reflect 包来访问程序的反射信息。

reflect.TypeOf()reflect.TypeNumFieldField
type User struct {
 Name string
 Age uint64
 Gender bool // true:男 false: 女
}


func main()  {
 u := User{
  Name: "asong",
  Age: 18,
  Gender: false,
 }
 getType := reflect.TypeOf(u)
 for i:=0; i < getType.NumField(); i++{
  fieldType := getType.Field(i)
  // 输出成员名
  fmt.Printf("name: %v \n", fieldType.Name)
 }
}
// 运行结果
name: Name 
name: Age 
name: Gender
Go
reflect.TypeOf()
func TypeOf(i interface{}) Type {
 eface := *(*emptyInterface)(unsafe.Pointer(&i))
 return toType(eface.typ)
}
Gointerface{}

一个空接口结构如下:

type eface struct {
    _type *_type
    data  unsafe.Pointer
}
_typedata实现了
TypeOf_type
NumField()
func (t *rtype) Kind() Kind { return Kind(t.kind & kindMask) }
func (t *rtype) NumField() int {
 if t.Kind() != Struct {
  panic("reflect: NumField of non-struct type " + t.String())
 }
 tt := (*structType)(unsafe.Pointer(t))
 return len(tt.fields)
}
structNumFiled()structpanicrtypestructType
// structType represents a struct type.
type structType struct {
 rtype
 pkgPath name
 fields  []structField // sorted by offset
}
// Struct field
type structField struct {
 name        name    // name is always non-empty
 typ         *rtype  // type of field
 offsetEmbed uintptr // byte offset of field<<1 | isEmbedded
}
Field()panic
func (t *rtype) Field(i int) StructField {
  // 类型检查
 if t.Kind() != Struct {
  panic("reflect: Field of non-struct type " + t.String())
 }
  // 强制转换成structType 类型
 tt := (*structType)(unsafe.Pointer(t))
 return tt.Field(i)
}
// Field returns the i'th struct field.
func (t *structType) Field(i int) (f StructField) {
  // 溢出检查
 if i < 0 || i >= len(t.fields) {
  panic("reflect: Field index out of bounds")
 }
 // 获取之前structType中fields字段的值
 p := &t.fields[i]
  // 转换成StructFiled结构体
 f.Type = toType(p.typ)
 f.Name = p.name.name()
  // 判断是否是匿名结构体
 f.Anonymous = p.embedded()
 if !p.name.isExported() {
  f.PkgPath = t.pkgPath.name()
 }
 if tag := p.name.tag(); tag != "" {
  f.Tag = StructTag(tag)
 }
  // 获取字段的偏移量
 f.Offset = p.offset()
  // 获取索引值
 f.Index = []int{i}
 return
}
StructField
// A StructField describes a single field in a struct.
type StructField struct {
   Name string // 字段名
   PkgPath string // 字段路径
   Type      Type      // 字段反射类型对象
   Tag       StructTag // 字段的结构体标签
   Offset    uintptr   // 字段在结构体中的相对偏移
   Index     []int     // Type.FieldByIndex中的返回的索引值
   Anonymous bool      // 是否为匿名字段
}

到这里整个反射获取结构体成员信息的过程应该很明朗了吧~。

Go实现了structTypeStructField

总结

Go