implementing Generics in Go 1.18 - Type Inference scenarios for Slices of Interfaces
I'm a bit lost with I'm optimizing some code but I'm currently trying to implement a generic function in Go 1.18 that accepts a slice of interfaces and processes them... However, I'm working with type inference issues when trying to pass a slice of a specific interface to my generic function. Here's a simplified version of my code: ```go package main import "fmt" type Processor[T any] func(T) type MyInterface interface { DoSomething() } type MyStruct struct { name string } func (m MyStruct) DoSomething() { fmt.Println("Doing something with", m.name) } func ProcessItems[T MyInterface](items []T, processor Processor[T]) { for _, item := range items { processor(item) } } func main() { items := []MyStruct{{"Item1"}, {"Item2"}} ProcessItems(items, func(m MyStruct) { m.DoSomething() }) } ``` When I run this, I receive the following behavior: ``` ./main.go:19:6: want to use items (variable of type []MyStruct) as []T value in argument to ProcessItems: []MyStruct does not implement MyInterface ``` I am aware that generics can be tricky with interfaces, but I'm not sure why this is happening. I've tried explicitly casting `items` to `[]MyInterface`, but that results in another type mismatch. Is there a way to make this work without losing the type safety that generics provide? Any insights or solutions would be greatly appreciated! Any feedback is welcome! Thanks in advance!