一、依赖注入
package main

import "fmt"

func a1()  {
	fmt.Println("a1调用")
}
func a2()  {
	fmt.Println("a2调用")
}
func main() {
	//通过map实现依赖注入
	myMap := make(map[string]func())
	myMap["a1"] = a1
	myMap["a2"] = a2
	//通过map的key调用函数
	myMap["a1"]()
	myMap["a2"]()
}

二、依赖注入框架

下载第三方包

三、使用 inject调用函数
package main

import (
	"fmt"
	"github.com/codegangsta/inject"
)

type S1 interface {}
type S2 interface {}

func Format(name string,company S1,level S2,age int)  {
	fmt.Printf("name = %s,company = %s,level = %s,age = %d!\n",name,company,level,age)
}
func main() {
   //创建控制实例
	injector := inject.New()
	//实参注入
	injector.Map("tom")
	injector.MapTo("tencent",(*S1)(nil))
	injector.MapTo("T4",(*S2)(nil))
	injector.Map(12)
	//反转调用函数
	injector.Invoke(Format)
}

 四、inject操作结构体
package main

import (
	"fmt"
	"github.com/codegangsta/inject"
)

type S1 interface {}
type S2 interface {}
type Staff struct {
	Name string `inject`
	Company S1 `inject`
	Level S2 `inject`
	Age int `inject`
}

func main() {
	staff := Staff{}
	//创建控制实例
	injector := inject.New()
	//实参注入
	injector.Map("tom")
	injector.MapTo("tencent",(*S1)(nil))
	injector.MapTo("T4",(*S2)(nil))
	injector.Map(12)
	//反转调用函数
	injector.Apply(&staff)
	fmt.Println(staff)
}

 五、抓取信息
package main

import (
	"fmt"
	"github.com/codegangsta/inject"
)

type S1 interface {}

func main() {
	fmt.Println(inject.InterfaceOf((*interface{})(nil)))
	fmt.Println(inject.InterfaceOf((*S1)(nil)))
}