Determine the type of a variable in Go
Introduction
Example
package main import ( "fmt" "reflect" ) func main() { var_str := "string" var_int := 10 var_float64 := 1.2 var_bool := true var_complex128 := complex(2, 3) r, im := real(var_complex128), imag(var_complex128) fmt.Println(reflect.TypeOf(var_str)) fmt.Println(reflect.TypeOf(var_int)) fmt.Println(reflect.TypeOf(var_float64)) fmt.Println(reflect.TypeOf(var_bool)) fmt.Println(reflect.TypeOf(var_complex128)) fmt.Println(reflect.TypeOf(r), reflect.TypeOf(im)) }
string
int
float64
bool
complex128
float64 float64
In other languages
- C : there is no such function.
-
C++
:
a similar task is solved by the function
typeid()
Read the article: «How to determine the type of a C++ variable»
- C# : there is a similar function GetType()
-
Python
:
a similar function
type()
and a slightly different function
isinstance()
with which you can solve the same problem.
Read the article: «How to determine the type of a Python variable»
Article author: Andrei Olegovich