samber / do

⚙️ A dependency injection toolkit based on Go 1.18+ Generics.
https://pkg.go.dev/github.com/samber/do
MIT License
1.82k stars 75 forks source link

Need better handling for arrays #79

Closed d-enk closed 4 months ago

d-enk commented 4 months ago
package main

import (
    "fmt"

    "github.com/samber/do/v2"
    typeStr "github.com/samber/go-type-to-string" // v1.3.0
)

func main() {
    i := do.New()

    fmt.Println(typeStr.GetType[[1]int]()) // []int
    fmt.Println(do.NameOf[[1]int]())       // []int

    // but
    fmt.Println(reflect.TypeOf([1]int{1}).String()) // [1]int

    do.ProvideValue(i, [1]int{})
    fmt.Println(do.InvokeAs[[3]int](i)) // could not find service satisfying interface `[]int`, available services: `[]int`
    fmt.Println(do.InvokeAs[[]int](i))  // could not find service satisfying interface `[]int`, available services: `[]int`
    fmt.Println(do.InvokeAs[[]int](i))  // could not find service satisfying interface `[]int`, available services: `[]int`

    do.ProvideValue(i, []int{}) // panic: DI: service `[]int` has already been declared
}