1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| package main
import ( "fmt" "reflect" )
type Student struct { Name string `json:"name"` Age int `json:"age"` Score float32 `json:"分数"` Sex string }
func (s Student) GetSum(n1 int, n2 int) int { return n1 + n2 }
func (s Student) Set(name string, age int, score float32, sex string) { s.Name = name s.Age = age s.Score = score s.Sex = sex }
func (s Student) Print() { fmt.Println("---start~----") fmt.Println(s) fmt.Println("---end~----") }
func TestStruct(a interface{}) { aType := reflect.TypeOf(a) aVal := reflect.ValueOf(a) aKind := aVal.Kind()
if aKind != reflect.Struct { fmt.Println("操作失败,参数不是结构体") return }
fNum := aVal.NumField() fmt.Println("结构体字段数量", fNum)
for i := 0; i < fNum; i++ { fmt.Printf("字段 %d 的值为 %v\n", i, aVal.Field(i)) tagVal := aType.Field(i).Tag.Get("json") if tagVal != "" { fmt.Printf("字段 %d 的tag为 %v \n", i, tagVal) } }
mNum := aVal.NumMethod() fmt.Println("结构体方法数量", mNum)
aVal.Method(1).Call(nil)
var params []reflect.Value params = append(params, reflect.ValueOf(10)) params = append(params, reflect.ValueOf(40)) res := aVal.Method(0).Call(params) fmt.Println("res=", res[0].Int()) }
func main() { var s Student = Student { Name : "学生1", Age : 400, Score : 30.5, }
TestStruct(s) }
|