其他分享
首页 > 其他分享> > [Go] Types - Reflect.TypeOf()

[Go] Types - Reflect.TypeOf()

作者:互联网

Can use reflect.TypeOf to get variable type

package main

import (
	"fmt"
	"reflect"
)

func main() {

	// var age int = 21
	// var b bool = age >= 23
	var age = 21
	var b = age >= 23
	fmt.Println(reflect.TypeOf(age))                // int
	fmt.Println(reflect.TypeOf(float64(age) * 5.5)) // float64
	fmt.Println(reflect.TypeOf(b))                  // bool
}

 

Can use type as constructor to convert a different type:

float64(age) * 5.5

 

标签:reflect,age,Reflect,var,TypeOf,Go,type,fmt
来源: https://www.cnblogs.com/Answer1215/p/16655269.html