網站首頁 編程語言 正文
fmt.Printf("%T")方式
示例:
var1 := "hello world" fmt.Printf("var1 = %T\n", var1)
這個是最簡單,也是最推薦的在at rumtime時獲取變量類型的方式,
用fmt.Printf("%T")實現返回變量類型的函數
由此衍生出可復用獲取變量或者對象type的函數,如下面函數,直接返回變量類型
func typeofVariable(variable interface{}) string { return fmt.Sprintf("%T", variable) }
?reflect.TypeOf方式
示例:
var1 := "hello world" fmt.Printf("var1: %s\n", reflect.TypeOf(var1))
用? reflect包里的函數 TypeOf()來實現,用起來相對來說復雜些,如果只是單純地想在runtime時獲取一下變量類型,還是推薦第一種方式
用reflect.TypeOf實現返回變量類型的函數
由此衍生出可復用獲取變量或對象type的函數,如下面函數,直接返回變量類型
func typeofVariable(variable interface{}) string { return reflect.TypeOf(variable).String() }
reflect.ValueOf.Kind()方式
示例:
var1 := "hello world" fmt.Println(reflect.ValueOf(var1).Kind())
用 reflect.ValueOf.Kind()實現返回變量類型的函數
func typeofVariable(variable interface{}) string { return reflect.ValueOf(variable).Kind().String() }
斷言方式
func typeofObject(variable interface{}) string { switch variable.(type) { case string: return "string" case int: return "int" case float32: return "float32" case float64: return "float64" case bool: return "boolean" case []string: return "[]string" case complex128: return "complex128" case complex64: return "complex64" case map[string]float64: return "map" case Employee: return "Employee" default: return "unknown" } }
代碼示例:
package main import ( "fmt" "reflect" ) type Employee struct { name string age int salary float64 } // 4 ways to return a variable's type at runtime func typeofObject(variable interface{}) string { switch variable.(type) { case string: return "string" case int: return "int" case float32: return "float32" case float64: return "float64" case bool: return "boolean" case []string: return "[]string" case complex128: return "complex128" case complex64: return "complex64" case map[string]float64: return "map" case Employee: return "Employee" default: return "unknown" } } func main() { var1 := "hello world" var2 := 10 var3 := 2.55 var4 := []string{"BeiJing", "ShangHai", "ShenZhen"} var5 := map[string]float64{"BeiJing": 3.2, "ShaiHai": 1.2} var6 := complex(3,4) var7 := true var8 := Employee{"Sam",30,15000.5} fmt.Println("############### Using %T with Printf ########################") fmt.Printf("var1 = %T\n", var1) fmt.Printf("var2 = %T\n", var2) fmt.Printf("var3 = %T\n", var3) fmt.Printf("var4 = %T\n", var4) fmt.Printf("var5 = %T\n", var5) fmt.Printf("var6 = %T\n", var6) fmt.Printf("var7 = %T\n", var7) fmt.Printf("var8 = %T\n", var8) fmt.Println("###############Using reflect.TypeOf Function #######################") fmt.Printf("var1: %s\n", reflect.TypeOf(var1)) fmt.Printf("var2: %s\n", reflect.TypeOf(var2)) fmt.Printf("var3: %s\n", reflect.TypeOf(var3)) fmt.Printf("var4: %s\n", reflect.TypeOf(var4)) fmt.Printf("var5: %s\n", reflect.TypeOf(var5)) fmt.Printf("var6: %s\n", reflect.TypeOf(var6)) fmt.Printf("var7: %s\n", reflect.TypeOf(var7)) fmt.Printf("var8: %s\n", reflect.TypeOf(var8)) fmt.Println("###############Using reflect.ValueOf.Kind() ########################") fmt.Println(reflect.ValueOf(var1).Kind()) fmt.Println(reflect.ValueOf(var2).Kind()) fmt.Println(reflect.ValueOf(var3).Kind()) fmt.Println(reflect.ValueOf(var4).Kind()) fmt.Println(reflect.ValueOf(var5).Kind()) fmt.Println(reflect.ValueOf(var6).Kind()) fmt.Println(reflect.ValueOf(var7).Kind()) fmt.Println(reflect.ValueOf(var8).Kind()) fmt.Println("################## Using Type assertions ###########################") fmt.Println(typeofObject(var1)) fmt.Println(typeofObject(var2)) fmt.Println(typeofObject(var3)) fmt.Println(typeofObject(var4)) fmt.Println(typeofObject(var5)) fmt.Println(typeofObject(var6)) fmt.Println(typeofObject(var7)) fmt.Println(typeofObject(var8)) } /* 幾個復用的函數來判斷變量的類型at runtime // Using %T func typeofVariable(variable interface{}) string { return fmt.Sprintf("%T", variable) } // Using reflect.TypeOf() func typeofVariable(variable interface{}) string { return reflect.TypeOf(variable).String() } // Using reflect.ValueOf().Kind() func typeofVariable(variable interface{}) string { return reflect.ValueOf(variable).Kind().String() } */
結果演示:
總結
原文鏈接:https://blog.csdn.net/u011285281/article/details/127516257
相關推薦
- 2022-09-26 利用QDir實現刪除選定文件目錄下的空文件夾_C 語言
- 2022-09-23 Python線程threading(Thread類)_python
- 2022-03-05 Linux下Apache服務的部署和配置_Linux
- 2022-05-10 thymeleaf給響應頁面傳遞參數(modelandview 中的model)
- 2022-08-03 python中的三種注釋方法_python
- 2023-06-03 Python+Requests+PyTest+Excel+Allure?接口自動化測試實戰_pyth
- 2023-02-14 C++深入分析數據在內存中的存儲形態_C 語言
- 2022-06-02 Go語言的變量定義詳情_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支