How to check type of variable in golang?

Member

anahi.murazik

by anahi.murazik , in category: Golang , 23 days ago

How to check type of variable in golang?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by katharina , 23 days ago

@anahi.murazik You can import reflect package and pass your variable inside and call the method Kind() on it in Golang, some example below how it works:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
   "fmt"
   "reflect"
)

func main() {
   integer := 4

   fmt.Println(reflect.TypeOf(integer).Kind()) // int

   varString := "computertips"

   fmt.Println(reflect.TypeOf(varString).Kind()) // string

   varBoolean := false

   fmt.Println(reflect.TypeOf(varBoolean).Kind()) // bool
}