You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When accessing an element from a slice via reflection (ie. via the Index method of reflect.Value), the returned reflect.Value's type is the slice's Elem type, not the actual type of the value.
ie.
var x = []any{string("")}
reflect.ValueOf(x).Index(0).Type() // returns "interface {}" instead of "string"
This is working as intended. For the second example, passing interface types to ValueOf results in passing the underlying type as interfaces don't nest.
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.
Go version
go1.22-20240109-RC01 linux/amd64
Output of
go env
in your module/workspace:What did you do?
When accessing an element from a slice via reflection (ie. via the
Index
method ofreflect.Value
), the returnedreflect.Value
's type is the slice'sElem
type, not the actual type of the value.ie.
This seems strange behavior, especially since
See https://go.dev/play/p/YNfd_PClNWe
What did you see happen?
What did you expect to see?
I'd expect the
Index
method to return the actual underlying value of the slice, so its Type method should point to the actual value type.The text was updated successfully, but these errors were encountered: