如何确定类型是否是Golang中的结构

Fer*_*nto 2 reflection struct go

假设我有2个结构:

type Base struct {
 id int
 name string
}

type Extended struct {
 Base
 Email string
 Password string
}
Run Code Online (Sandbox Code Playgroud)

我想反映扩展结构来获取它的领域:

e := Extended{}
e.Email = "me@mail.com"
e.Password = "secret"

for i := 0 ; i < reflect.TypeOf(e).NumField() ; i++ {
  if reflect.TypeOf(e).Field(i) != "struct" {  << how to do this validation?
    fmt.Println(reflect.ValueOf(e).Field(i))
  }
}
Run Code Online (Sandbox Code Playgroud)

Uve*_*tel 10

只需检查Value的Kind()

if reflect.ValueOf(e).Field(i).Kind() != reflect.Struct {
    fmt.Println(reflect.ValueOf(e).Field(i))
}
Run Code Online (Sandbox Code Playgroud)