Go语言高级特性与软件分析全解析
1. 反射(Reflection)
反射是Go语言的一项高级特性,它能让你动态地了解任意对象的类型及其结构信息。借助 reflect 包和 reflect.TypeOf() 函数,程序可以获取对象的类型信息。下面通过 reflection.go 程序来详细说明:
package main
import (
"fmt"
"reflect"
)
func main() {
type t1 int
type t2 int
x1 := t1(1)
x2 := t2(1)
x3 := 1
st1 := reflect.ValueOf(&x1).Elem()
st2 := reflect.ValueOf(&x2).Elem()
st3 := reflect.ValueOf(&x3).Elem()
typeOfX1 := st1.Type()
typeOfX2 := st2.Type()
typeOfX3 := st3.Type()
fmt.Printf("X1 Type: %s\n", typeOfX1)
fmt.Printf("X2 Type: %s\n", typeOfX2)
fmt.Printf("X3 Type: %s\n", typeOfX3)
type aStructure struct {
X
超级会员免费看
订阅专栏 解锁全文
1296

被折叠的 条评论
为什么被折叠?



