dimanche 27 septembre 2020

Golang check type a variable

I'm making a call to os/Create function and want to make sure in one of my test cases that the response is indeed of type *os.File.

Below is my code snippet. Though I made a lot of iterations but the motivation for these lines was this post.

//somevar -- gets *os.File from a function
var varType *os.File
tpe := reflect.TypeOf(varType).Elem()
fmt.Println(reflect.TypeOf(somevar).Implements(tpe)) // I expect a true or false

When I run this code I get a panic:

panic: reflect: non-interface type passed to Type.Implements [recovered]
    panic: reflect: non-interface type passed to Type.Implements

Please suggest what wrong I'm doing. All I want to check for is - some variable is of type *os.File - yes or no.

Thank you





Aucun commentaire:

Enregistrer un commentaire