Home > Article > Backend Development > How to determine data type in golang
Use Go's empty interface:
i.(type) can only be used in switch, the function has no return value
func m_type(i interface{}) { switch i.(type) { case string: //... case int: //... } return}
Use reflection:
reflect.TypeOf(x)
package main import ( "fmt" "reflect" ) func main() { var x int32 = 20 fmt.Println("type:", reflect.TypeOf(x)) }
Summary: The first method requires knowing how many types there are, and the second method can be used for any object.
Recommended to study "golang tutorial"
The above is the detailed content of How to determine data type in golang. For more information, please follow other related articles on the PHP Chinese website!