We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I am trying to understand why this code works:
ioReaderType := reflect.ValueOf(struct{ ioReader io.Reader }{}).FieldByName("ioReader").Type() fmt.Println(ioReaderType.String()) // prints "io.Reader"
But this one panic
ioReaderType := reflect.TypeOf(io.Reader(nil)) fmt.Println(ioReaderType.String()) // panic
https://play.golang.org/p/F2ndRax1f4E
The text was updated successfully, but these errors were encountered:
From the doc of reflect.TypeOf:
reflect.TypeOf
TypeOf returns the reflection Type that represents the dynamic type of i. If i is a nil interface value, TypeOf returns nil.
io.Reader(nil) is a nil interface value. Castingnil to an interface type is still nil.
io.Reader(nil)
nil
See the example for reflect.TypeOf. You want to instead do:
reflect.TypeOf((*io.Reader)(nil)).Elem()
Casting a nil pointer to an interface type is not nil.
Sorry, something went wrong.
No branches or pull requests
I am trying to understand why this code works:
But this one panic
https://play.golang.org/p/F2ndRax1f4E
The text was updated successfully, but these errors were encountered: