r1笔记day20_03 Go reflect 2

本文通过实例讲解如何使用Go语言的反射机制修改int值及操作结构体。介绍了如何利用Elem()方法修改变量值,以及如何通过反射查看结构体的字段和方法数量,并调用结构体的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面通过修改int值的例子,学习type Value,以及提供的方法Elem(),这个方法就相当于提供了指针,并指向了这个变量,就可以修改值了。

利用反射操作Int,比如说修改int的值

package main

import (
	"fmt"
	"reflect"
)

//利用反射,修改值,提供了Elem(),这个方法,相当于一个指针指向变量
func TestInt(b interface{}) {
	val := reflect.ValueOf(b) //返回Value这个类型,接下来很多方法可以分析
	val.Elem().SetInt(100)    //elem这个方法相当于指针指向变量,得到指针指向的变量,才可以操作

	c := val.Elem().Int()
	fmt.Printf("get value interface{} %d \n", c)
	fmt.Printf("string val:%d \n", val.Elem().Int())
}

func main() {

	var cc int = 1
	TestInt(&cc)
	fmt.Println(cc)
}

利用反射查看一个结构体相关信息:比如有多少fileds,有多少method,同时,调用方法的方式。

package main

import (
	"fmt"
	"reflect"
)

type Student struct {
	Name  string
	Age   int
	score float32
}

//因为是*Student,method不会统计指针的这个方法
func (s *Student) SetPointer() {
}

//下面两个接受者是值,所以统计method时候有效
func (s Student) SetValue1() {
	fmt.Println("----------start-------------")
	fmt.Println("SetValue1")
	fmt.Println("----------end---------------")
}

func (s Student) SetValue2() {
	fmt.Println("----------start-------------")
	fmt.Println("SetValue2")
	fmt.Println("----------end---------------")
}

func TestStruct(a interface{}) {
	val := reflect.ValueOf(a)
	if kd := val.Kind(); kd != reflect.Struct {
		fmt.Println("unexpect struct", kd)
		return
	}
	num := val.NumField()
	fmt.Printf("struct has %d fields \n", num)

	numOfMethod := val.NumMethod()
	fmt.Printf("struct has %d methods \n", numOfMethod)

	//用反射调用结构体的方法
	var params []reflect.Value
	val.Method(0).Call(params)
	val.Method(1).Call(params)
}

func main() {
	var a Student = Student{
		Name:  "stu1",
		Age:   18,
		score: 99,
	}
	TestStruct(a)
	//传入一个int测试
	var b int
	TestStruct(b)
}

运行结果:

PS F:\go\src\go_dev> .\main.exe
struct has 3 fields
struct has 2 methods
----------start-------------
SetValue1
----------end---------------
----------start-------------
SetValue1
----------end---------------
unexpect struct int
PS F:\go\src\go_dev>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值