Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use CanConvert() of reflect.Value in golang

I am new in golang

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

>Solution :

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...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading