package main

import (
    "fmt"
    "reflect"
    "time"
)

type BaseModel struct {
    // TODO 分布式ID 雪花算法 https://www.itqiankun.com/article/1565747019
    ID        int64     `xorm:"pk unique"`
    CreatedAt time.Time `xorm:"created"`
    UpdatedAt time.Time `xorm:"updated"`
}
type Product struct {
    BaseModel
    Title string
    Price float64
}

func main() {
    //  prod := Product{Title: "商品标题", Price: 19.6, ID: 1507252296864501760} 错误写法
    prod := Product{Title: "商品标题", Price: 19.6}
    prod.ID = 1507252296864501760
    fmt.Println(prod)
    typeof := reflect.TypeOf(prod)
    valueof := reflect.ValueOf(prod)
    for i := 0; i < typeof.NumField(); i++ {
        field := typeof.Field(i)
        value := valueof.Field(i).Interface()
        fmt.Printf("\n---[%d]StructField-Name(%s)-TypeName(%s)-TypeKind(%s)-Value(%+v)\n", i, field.Name, field.Type.Name(), field.Type.Kind(), value)
    }

}

输出

{{1507252296864501760 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC} 商品标题 19.6}

---[0]StructField-Name(BaseModel)-TypeName(BaseModel)-TypeKind(struct)-Value({ID:1507252296864501760 CreatedAt:0001-01-01 00:00:00 +0000 UTC UpdatedAt:0001-01-01 00:00:00 +0000 UTC})

---[1]StructField-Name(Title)-TypeName(string)-TypeKind(string)-Value(商品标题)

---[2]StructField-Name(Price)-TypeName(float64)-TypeKind(float64)-Value(19.6)
.Elem()
typeof := reflect.TypeOf(ptrr).Elem() // ptrr为指针类型
valuesof := reflect.ValueOf(ptrr).Elem()