Go provides several methods to check the type of variables, format the string to identify %T, reflection method:, and also use type assertions, switch case method. The following are examples to introduce these four types of methods.
%T Format ID
Formatting the identifier with %T string is the easiest way to check types. %T is the fmt package, and you can use the display 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 ("variable count=%v is of type %T \n", count, count) ("variable message='%v' is of type %T \n", message, message) ("variable isCheck='%v' is of type %T \n", isCheck, isCheck) ("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 reflect package function
If the above method is not useful, or 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 also supports passing variable values directly instead of variables. The code is as follows:
("%v", (10)) //int ("%v", ("Go Language")) //string
Here 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 ("variable days=%v is of type %v \n", days, (days)) ("variable typemessage='%v' is of type %v \n", typemessage, (typemessage)) ("variable isFound='%v' is of type %v \n", isFound, (isFound)) ("variable objectValue=%v is of type %v \n", objectValue, (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
().Kind()
Similarly, using ValueOf().Kind() can also obtain the type of variable. () Returns the new value based on the passed 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 ("variable days=%v is of type %v \n", days, (days).Kind()) ("variable typemessage='%v' is of type %v \n", typemessage, (typemessage).Kind()) ("variable isFound='%v' is of type %v \n", isFound, (isFound).Kind()) ("variable objectValue=%v is of type %v \n", objectValue, (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 approach is that new variables are generated, which may increase memory footprint.
Use type assertion
Another approach in this section is type assertion. Here is a method typeofObject for 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" } } ("Using type assertions") (typeofObject(count)) (typeofObject(message)) (typeofObject(isCheck)) (typeofObject(amount)) //OUTPUT Using type assertions int string boolean float64
The advantage of this method is that the types can be grouped, for example, we can identify all int32, int64, uint32, uint64 types as "int".
Custom method check type
Through the above method, we can write our own method to implement code reuse:
// Using %T func typeofObject(variable interface{}) string { return ("%T", variable) } // Using () func typeofObject(variable interface{}) string { return (variable).String() } // Using ().Kind() func typeofObject(variable interface{}) string { return (variable).Kind().String() }
The following is called the above method for testing:
(typeofObject(count)) (typeofObject(message)) (typeofObject(isCheck)) (typeofObject(amount)) //OUTPUT Using type assertions int string boolean float64
This is the end of this article about several methods of Golang checking variable types. For more relevant content on go to check variable types, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!