Go语言教程之边写边学:Golang中的反射:Reflect包的ValueOf函数

ValueOf函数来创建一个reflect。表示变量值的Value实例。

 

示例代码:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	v1 := []int{1, 2, 3, 4, 5}
	fmt.Println(reflect.ValueOf(v1))

	v2 := "Hello World"
	fmt.Println(reflect.ValueOf(v2))

	v3 := 1000
	fmt.Println(reflect.ValueOf(v3))
	fmt.Println(reflect.ValueOf(&v3))

	v4 := map[string]int{"mobile": 10, "laptop": 5}
	fmt.Println(reflect.ValueOf(v4))

	v5 := [5]int{1, 2, 3, 4, 5}
	fmt.Println(reflect.ValueOf(v5))

	v6 := true
	fmt.Println(reflect.ValueOf(v6))
}

 

输出:

[1 2 3 4 5]
Hello World
1000
0xc0000a00b8
map[laptop:5 mobile:10]
[1 2 3 4 5]
true
  • 当前日期:
  • 北京时间:
  • 时间戳:
  • 今年的第:19周
  • 我的 IP:3.137.178.51
农历
五行
冲煞
彭祖
方位
吉神
凶神
极简任务管理 help
+ 0 0 0
Task Idea Collect