Home > Blockchain >  How to use CanConvert() of reflect.Value
How to use CanConvert() of reflect.Value

Time:11-12

I have a reflect.Value type of data and I want to check if the value can be convert to uint or not.

this is just an example hope you guys get the idea

var myVal = new(reflect.Value)
if myVal.CanConvert(uint) { // this doesn't work
   // do stuf...
}

I dont know what I have to pass as the argument of CanConvert()

CodePudding user response:

The argument to the CanConvert method is a reflect.Type. Use the reflect.TypeOf function to get a reflect.Type from a value of the type.

if myVal.CanConvert(reflect.TypeOf(uint(0)) { 
   // do stuff...
}
  •  Tags:  
  • go
  • Related