[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

 

posted @ 2022-09-04 15:52  Zhentiw  阅读(26)  评论(0编辑  收藏  举报