获取变量地址
package main
import (
"fmt"
"reflect"
)
func main() {
x := 2
a := reflect.ValueOf(2)
b := reflect.ValueOf(x)
c := reflect.ValueOf(&x)
d := c.Elem()
fmt.Printf("a:%T,%v\n", a, a)
fmt.Printf("b:%T,%v\n", b, b)
fmt.Printf("c:%T,%v\n", c, c)
fmt.Printf("d:%T,%v\n", d, d)
fmt.Printf("-------test CanAddr------\n")
fmt.Println(a.CanAddr())
fmt.Println(b.CanAddr())
fmt.Println(c.CanAddr())
fmt.Println(d.CanAddr())
}
运行结果:
a:reflect.Value,2
b:reflect.Value,2
c:reflect.Value,0x10b220ac
d:reflect.Value,2
-------test CanAddr------
false
false
false
true
成功: 进程退出代码 0.