Convert a variable to a reflect. Type

Asked

Viewed 66 times

0

I started learning Go recently. I want to be able to save one type of data into one variable, and convert another variable to it. I know that one way to save a data type is by using reflect.Type.

i := 5
t := reflect.TypeOf(i)
var f float32 = 4.2

But how do I convert f for the guy saved in t?

  • 1

    Do you really need to use the reflect package or do you just want to do a simple type conversion? Wouldn’t that be enough? https://go-tour-br.appspot.com/basics/13

  • @navossoc The idea is to convert to a type dynamically, which will be saved in the class

  • 1

    See if it looks like this: https://goplay.space/#Mxuau0dbmq Anyway, I don’t understand where you’re going with this.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.