Find Out Golang Variable Type

How to find out the variable type of the variable you are using in Go/Golang? I had this issue when I was using Bolt DB package and tried to share the connection by passing the db variable around.

To return it, your function needs to declare the variable type it is returning. To pass it into a function, the function needs to declare the variable type being passed in. To find out the variable type, I used the reflect package.

package mainimport (
"fmt"
"reflect"
)
func main() {
s := "golang"
i := 1
f := 3.142

fmt.Println(reflect.TypeOf(s))
fmt.Println(reflect.TypeOf(i))
fmt.Println(reflect.TypeOf(f))
}

The output of the codes above will be

string
int
float64

< Back to all the stories I had written

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store