golangで変数が文字列かどうかを検出する方法
変数が文字列であるかどうかを検出する方法: 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. 型アサーションを使用して型をグループ化します。
このチュートリアルの動作環境: 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 サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Go Crawler Collyのキュースレッドの問題は、Go言語でColly Crawler Libraryを使用する問題を調査します。 �...

大企業または有名なオープンソースプロジェクトによって開発されたGOのどのライブラリが開発されていますか? GOでプログラミングするとき、開発者はしばしばいくつかの一般的なニーズに遭遇します...

GO言語で構造を定義する2つの方法:VARとタイプのキーワードの違い。構造を定義するとき、GO言語はしばしば2つの異なる執筆方法を見ます:最初...

Go言語での文字列印刷の違い:printlnとstring()関数を使用する効果の違いはGOにあります...

Golandのカスタム構造ラベルが表示されない場合はどうすればよいですか?ゴーランドを使用するためにGolandを使用する場合、多くの開発者はカスタム構造タグに遭遇します...

redisstreamを使用してGo言語でメッセージキューを実装する問題は、GO言語とRedisを使用することです...

マルチプロセスのログライティングの並行性セキュリティの問題を効率的に処理します。複数のプロセスが同じログファイルを同時に書き込みます。並行性が安全で効率的であることを確認する方法は?これは...
