How to detect whether a variable is a string in golang
Method to detect whether a variable is a string: 1. Use the "%T" formatting identifier, the syntax "fmt.Printf("variable count=%v is of type %T \n", count, count )"; 2. Use reflect.TypeOf(), the syntax "reflect.TypeOf(variable)"; 3. Use reflect.ValueOf().Kind() for detection; 4. Use type assertions to group types.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Golang detects whether a variable is a string by checking the type of the variable. Here are several methods.
Go provides several methods to check the type of variables, including the string formatting identifier %T, reflection methods: reflect.TypeOf, reflect.ValueOf.Kind, and the use of type assertions and switch case methods. These four types of methods are introduced below through examples.
%T formatting identifier
Using the %T string formatting identifier is the simplest way to check the type. %T is the fmt package. You can use fmt.Printf to display the variable type:
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
Use the reflect package function
If the above method does not work, Or if you want to get more information about the type, you can use the TypeOf and ValueOf().Kind functions of the reflect package.
reflect.TypeOf()
If you pass a variable value to the TypeOf method, the variable type will be returned. Of course, variables can also be passed, but it is also supported to pass variable values directly instead of variables. The code is as follows:
fmt.Printf("%v", reflect.TypeOf(10)) //int fmt.Printf("%v", reflect.TypeOf("Go Language")) //string
The following is a complete example of different variable types:
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()
You can also use ValueOf().Kind() to get the type of the variable. reflect.ValueOf() returns a new value based on the passed-in variable, and then further obtains the variable type through the Kind method:
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
The disadvantage of this method is that new variables need to be generated, which may increase memory usage.
Using type assertions
This section introduces another method is type assertion. Write a method typeofObject below to perform type judgment:
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
The advantage of this method is that it can group types. For example, we can identify all int32, int64, uint32, and uint64 types as "int".
【Related recommendations: Go video tutorial, Programming teaching】
The above is the detailed content of How to detect whether a variable is a string in golang. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

Automatic deletion of Golang generic function type constraints in VSCode Users may encounter a strange problem when writing Golang code using VSCode. when...
