Home > Article > Backend Development > 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.
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
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.
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
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.
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!