I note that the following *does* work:

fmt.Println(slices.Index[[]interface{}, interface{}](mySlice, MyStruct{A: 
2, B: 3}))

So I think it's something to do with how type inference works. But the 
details of this are beyond my comprehension.
https://go.dev/ref/spec#Type_inference 

On Friday, 3 October 2025 at 10:04:07 UTC+1 [email protected] wrote:

> Hi fellow generic experts
>
> I'd like to ask why does the last line in the below Snippet 1 fails with 
> the error:
>
> ./prog.go:17:26: in call to slices.Index, S (type []interface{}) does not 
> satisfy ~[]E
>
> The third line is essentially the same line, but it works!
>
> Snippet 1: Full code in https://go.dev/play/p/PPdIaK08v5A
> ```
> func main() {
> mySlice := []interface{}{1, "a", MyStruct{A: 2, B: 3}}
> fmt.Println(slices.Index(mySlice, 1))
> fmt.Println(slices.Index(mySlice, "a"))
> fmt.Println(slices.Index(mySlice, interface{}(MyStruct{A: 2, B: 3})))
>
> // Below does not work.
> // fmt.Println(slices.Index(mySlice, MyStruct{A: 2, B: 3}))
> }
> ```
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/23fd70c7-67a6-428e-81bf-22ccafe2feccn%40googlegroups.com.

Reply via email to