通用方法和建议如何消除依赖

I have a question regarding dependency injection. Please consider the example below.

For example, selector() is a function that select something and guarantee return an interface

In this example

bar.node.go

type NodeTemplate struct {
     Name string
}

// satisfy interface declared in db.foo.go 
//but never imports anything from db.foo.go
func (node *NodeTemplate) GetUuidName() string {
   if node != nil {
      return node.Name
   }
   return 
}

db.foo.go

// interface declared in db.foo.go

type Node interface {
   GetUuidName() string
}

Option A // So selector receives a map of Some interface and populate a map

func SelectSomething(nodemap map[string]Node, selectFactor string) {
     // selection from db and result populate in a map
}

Option B

Another pattern SelectSomething return a Node and it Interface

So another package will depend on importing Node and that will introduce a dependency.

func SelectSomething(seleconsomething) []*Node {
  // do selection and return a slice of SomeInterface

n := &Node{} // here it need to be concret type T
return Node
}

So based on logic I've described I see the first approach is better but in that approach, select need do concrete type allocation in order to populate a map.

Consider another example

db.foo.go

type Node interface {
   GetUuidName() string
}

func inserter(node *Node) error {
   // do some work 
   node.GetUuidName()
}

For a case like in inserter case, inserter has no external dependency, inserter just needs to receive something that satisfies the interface. Declare interfaces locally and that brake a dependancy.

But in the case of selector example, it has to do memory allocation in order to return or populate a map or return something that has concrete type T. So in both case, it has to have internal re-presentation.

So here is my question can selector somehow at run time figure out a type it receives based on the interface and instantiate an object of that type and insert to a map as an interface or return a slice of the interface. ?

By doing so selector function will have no dependancy on what it receives it just guarantee it will instantiate the same object type T and return interface.

or can selector return interface but I guess I have to have a bi-directional interface between db package and package X or dynamic dispatcher need to do some magic ?

You want a type to behave in a certain way. That is achieved via an interface. This case is no different. Simply add the desired behavior to your interface, as demonstrated below with the Foo interface.

package main

import (
    "fmt"
    "reflect"
)

type Foo interface {
    Bar()
    TypeOf() reflect.Type
}

type Baz struct{}

func (b Baz) Bar() {
    fmt.Println("I am a Fooer!")
}

func (b Baz) TypeOf() reflect.Type {
    return reflect.TypeOf(b)
}

func DoSomeThing(f Foo) {
    f.Bar()
    fmt.Println(f.TypeOf())
}

func main() {
    fmt.Println("Hello, playground")
    b := Baz{}
    DoSomeThing(b)
}

Run on playground