首頁 後端開發 Golang golang怎麼偵測變數是否為字串

golang怎麼偵測變數是否為字串

Jan 06, 2023 pm 12:41 PM
golang go語言 字串

偵測變數是否為字串的方法:1、利用「%T」格式化標識,語法「fmt.Printf("variable count=%v is of type %T \n", count, count )」;2、利用reflect.TypeOf(),語法「reflect.TypeOf(變數)」;3、利用reflect.ValueOf().Kind()偵測;4、使用型別斷言,可以對型別進行分組。

golang怎麼偵測變數是否為字串

本教學操作環境: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」。

【相關推薦:Go影片教學程式設計教學

以上是golang怎麼偵測變數是否為字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

Go語言中用於浮點數運算的庫有哪些? Go語言中用於浮點數運算的庫有哪些? Apr 02, 2025 pm 02:06 PM

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

Go的爬蟲Colly中Queue線程的問題是什麼? Go的爬蟲Colly中Queue線程的問題是什麼? Apr 02, 2025 pm 02:09 PM

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

在 Go 語言中,為什麼使用 Println 和 string() 函數打印字符串會出現不同的效果? 在 Go 語言中,為什麼使用 Println 和 string() 函數打印字符串會出現不同的效果? Apr 02, 2025 pm 02:03 PM

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

GoLand中自定義結構體標籤不顯示怎麼辦? GoLand中自定義結構體標籤不顯示怎麼辦? Apr 02, 2025 pm 05:09 PM

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

在Go語言中使用Redis Stream實現消息隊列時,如何解決user_id類型轉換問題? 在Go語言中使用Redis Stream實現消息隊列時,如何解決user_id類型轉換問題? Apr 02, 2025 pm 04:54 PM

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

Go語言中哪些庫是由大公司開發或知名的開源項目提供的? Go語言中哪些庫是由大公司開發或知名的開源項目提供的? Apr 02, 2025 pm 04:12 PM

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

Go語言中`var`和`type`關鍵字定義結構體的區別是什麼? Go語言中`var`和`type`關鍵字定義結構體的區別是什麼? Apr 02, 2025 pm 12:57 PM

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

多進程日誌寫入如何保證並發安全又高效? 多進程日誌寫入如何保證並發安全又高效? Apr 02, 2025 pm 03:51 PM

高效處理多進程日誌寫入的並發安全問題多進程同時寫入同一個日誌文件,如何保證並發安全且高效?這是一個...

See all articles