ホームページ  >  記事  >  バックエンド開発  >  golangで変数が文字列かどうかを検出する方法

golangで変数が文字列かどうかを検出する方法

青灯夜游
青灯夜游オリジナル
2023-01-06 12:41:103344ブラウズ

変数が文字列であるかどうかを検出する方法: 1. "%T" 書式設定識別子、構文 "fmt.Printf("variable count=%v is of type %T \n", count を使用します) , count )"; 2. 構文 "reflect.TypeOf(variable)" である、reflect.TypeOf() を使用します; 3. 検出には、reflect.ValueOf().Kind() を使用します; 4. 型アサーションを使用して型をグループ化します。

golangで変数が文字列かどうかを検出する方法

このチュートリアルの動作環境: Windows 7 システム、GO バージョン 1.18、Dell G3 コンピューター。

Golang は、変数の型をチェックすることで、変数が文字列であるかどうかを検出します。

Go には、文字列書式設定識別子 %T、リフレクション メソッド (reflect.TypeOf、reflect.ValueOf.Kind)、型アサーションと switch case メソッドの使用など、変数の型をチェックするためのメソッドがいくつか用意されています。以下にこれら 4 種類の方法を例を挙げて紹介します。

%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 パッケージ関数を使用します

If上記の方法は機能しません。または、型に関する詳細情報を取得したい場合は、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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。