golang怎麼偵測變數是否為字串
偵測變數是否為字串的方法:1、利用「%T」格式化標識,語法「fmt.Printf("variable count=%v is of type %T \n", count, count )」;2、利用reflect.TypeOf(),語法「reflect.TypeOf(變數)」;3、利用reflect.ValueOf().Kind()偵測;4、使用型別斷言,可以對型別進行分組。
本教學操作環境:windows7系統、GO 1.18版本、Dell G3電腦。
golang偵測變數是否為字串,就是檢查變數的類型,以下介紹幾種方法。
Go提供幾種方法檢查變數的類型,在字串格式化識別%T, 反射方式:reflect.TypeOf, reflect.ValueOf.Kind,另外還有使用類型斷言,switch case方式。以下透過實例分別介紹這四類方法。
%T 格式化識別
使用%T字串格式化識別是最簡單的檢查類型方式。 %T是fmt包,可以使用fmt.Printf顯示變數類型:
import ( "fmt" ) func main() { var count int = 42 var message string = "go find type" var isCheck bool = true var amount float32 = 10.2 fmt.Printf("variable count=%v is of type %T \n", count, count) fmt.Printf("variable message='%v' is of type %T \n", message, message) fmt.Printf("variable isCheck='%v' is of type %T \n", isCheck, isCheck) fmt.Printf("variable amount=%v is of type %T \n", amount, amount) } //OutPut variable count=42 is of type int variable message='go find type' is of type string variable isCheck='true' is of type bool variable amount=10.2 is of type float32
#使用reflect包函數
如果上述方法沒有用,或希望獲得類型的更多信息,可以使用reflect包的TypeOf和ValueOf().Kind函數。
reflect.TypeOf()
如果給TypeOf方法傳入變數值,則會傳回變數類型。當然也可以傳遞變量,但也支援直接傳遞變數值,而不是變量,程式碼如下:
fmt.Printf("%v", reflect.TypeOf(10)) //int fmt.Printf("%v", reflect.TypeOf("Go Language")) //string
下面是不同的變數類型的完整範例:
package main import ( "fmt" "reflect" ) func main() { var days int = 42 var typemessage string = "go find type" var isFound bool = false var objectValue float32 = 10.2 fmt.Printf("variable days=%v is of type %v \n", days, reflect.TypeOf(days)) fmt.Printf("variable typemessage='%v' is of type %v \n", typemessage, reflect.TypeOf(typemessage)) fmt.Printf("variable isFound='%v' is of type %v \n", isFound, reflect.TypeOf(isFound)) fmt.Printf("variable objectValue=%v is of type %v \n", objectValue, reflect.TypeOf(objectValue)) } //OUTPUT variable days=42 is of type int variable typemessage='go find type' is of type string variable isCheck='false' is of type bool variable amount=10.2 is of type float32 variable acounts=Savings is of type string
reflect.ValueOf().Kind()
同樣使用ValueOf().Kind()也可以得到變數的型別。 reflect.ValueOf()傳回基於傳入的變數初始化新值,然後進一步透過Kind方法獲得變數類型:
package main import ( "fmt" "reflect" ) func main() { var days int = 42 var typemessage string = "go find type" var isFound bool = false var objectValue float32 = 10.2 fmt.Printf("variable days=%v is of type %v \n", days, reflect.ValueOf(days).Kind()) fmt.Printf("variable typemessage='%v' is of type %v \n", typemessage, reflect.ValueOf(typemessage).Kind()) fmt.Printf("variable isFound='%v' is of type %v \n", isFound, reflect.ValueOf(isFound).Kind()) fmt.Printf("variable objectValue=%v is of type %v \n", objectValue, reflect.ValueOf(objectValue).Kind()) } //OUTPUT variable days=42 is of type int variable typemessage='go find type' is of type string variable isCheck='false' is of type bool variable objectValue=10.2 is of type float32
這種方法的缺點是需要產生新的變數,這可能會增加記憶體佔用。
使用型別斷言
本節介紹另一種方法是型別斷言。下面寫一個方法typeofObject進行型別判斷:
func typeofObject(variable interface{}) string { switch variable.(type) { case int: return "int" case float32: return "float32" case bool: return "boolean" case string: return "string" default: return "unknown" } } fmt.Println("Using type assertions") fmt.Println(typeofObject(count)) fmt.Println(typeofObject(message)) fmt.Println(typeofObject(isCheck)) fmt.Println(typeofObject(amount)) //OUTPUT Using type assertions int string boolean float64
這種方法的優點是可以將型別分組,例如,我們可以將所有int32、int64、uint32、uint64型別標識為「int」。
以上是golang怎麼偵測變數是否為字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

Go語言中哪些庫是大公司開發或知名開源項目?在使用Go語言進行編程時,開發者常常會遇到一些常見的需求,�...

Go語言中結構體定義的兩種方式:var與type關鍵字的差異Go語言在定義結構體時,經常會看到兩種不同的寫法:一�...
