-
Notifications
You must be signed in to change notification settings - Fork 4
/
reflect5.go
39 lines (32 loc) · 1.05 KB
/
reflect5.go
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
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言高级开发与实战》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 知乎:https://www.zhihu.com/people/shirdonl
// 公众号:源码大数据
// 仓库地址:https://gitee.com/shirdonl/goAdvanced
// 仓库地址:https://github.com/shirdonl/goAdvanced
//++++++++++++++++++++++++++++++++++++++++
package main
import (
"fmt"
"reflect"
)
func main() {
var money float32 = 66.66
fmt.Println("指针原来的值是:", money)
// 通过reflect.ValueOf获取money中的reflect.Value
// 注意,参数必须是指针才能修改其值
pointer := reflect.ValueOf(&money)
newValue := pointer.Elem()
fmt.Println("指针的类型是:", newValue.Type())
fmt.Println("指针是否可设置:", newValue.CanSet())
// 重新赋值
newValue.SetFloat(88.88)
fmt.Println("指针的新值是:", money)
}
//指针原来的值是: 66.66
//指针的类型是: float32
//指针是否可设置: true
//指针的新值是: 88.88