I think the fact that literals rather than concretely typed variables is
significant. With variables, the slice creation continues to work, as does
a search call with a literal, but searches with variables with the same
value do not.
https://go.dev/play/p/ujmKL3mDyOz

On Fri, Oct 3, 2025, 12:59 'Brian Candler' via golang-nuts <
[email protected]> wrote:

> 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
> <https://groups.google.com/d/msgid/golang-nuts/23fd70c7-67a6-428e-81bf-22ccafe2feccn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CADgY6e8GnGnWvwMU3RUow9x%2BDvqJGi%2Bghx%3DqTM7yaEXzxhe-ZA%40mail.gmail.com.

Reply via email to