当前位置:网站首页>Go learning - dependency injection
Go learning - dependency injection
2022-07-06 00:15:00 【Duck boss】
One 、 Dependency injection
package main
import "fmt"
func a1() {
fmt.Println("a1 call ")
}
func a2() {
fmt.Println("a2 call ")
}
func main() {
// adopt map Implement dependency injection
myMap := make(map[string]func())
myMap["a1"] = a1
myMap["a2"] = a2
// adopt map Of key Call function
myMap["a1"]()
myMap["a2"]()
}
Two 、 Dependency injection framework
Download third party packages
3、 ... and 、 Use inject Call function
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() {
// Create a control instance
injector := inject.New()
// Parameter injection
injector.Map("tom")
injector.MapTo("tencent",(*S1)(nil))
injector.MapTo("T4",(*S2)(nil))
injector.Map(12)
// Reverse call function
injector.Invoke(Format)
}
Four 、inject Operating structure
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{}
// Create a control instance
injector := inject.New()
// Parameter injection
injector.Map("tom")
injector.MapTo("tencent",(*S1)(nil))
injector.MapTo("T4",(*S2)(nil))
injector.Map(12)
// Reverse call function
injector.Apply(&staff)
fmt.Println(staff)
}
5、 ... and 、 Grab information
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)))
}
边栏推荐
- Zhuan: in the future, such an organization can withstand the risks
- QT--线程
- Solve the problem of reading Chinese garbled code in sqlserver connection database
- Qt 一个简单的word文档编辑器
- After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
- 7.5 decorator
- wx.getLocation(Object object)申请方法,最新版
- 转:未来,这样的组织才能扛住风险
- C # input how many cards are there in each of the four colors.
- [day39 literature extensive reading] a Bayesian perspective on magnetic estimation
猜你喜欢
FFT learning notes (I think it is detailed)
Key structure of ffmpeg -- AVCodecContext
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
Initialize your vector & initializer with a list_ List introduction
Problems encountered in the database
妙才周刊 - 8
Qt QPushButton详解
Senparc.Weixin.Sample.MP源码剖析
MySql——CRUD
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
随机推荐
NSSA area where OSPF is configured for Huawei equipment
Detailed explanation of APP functions of door-to-door appointment service
7.5 decorator
【DesignMode】装饰者模式(Decorator pattern)
Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
提升工作效率工具:SQL批量生成工具思想
There is no network after configuring the agent by capturing packets with Fiddler mobile phones
Make a short video clip number of we media film and television. Where can I download the material?
微信小程序---WXML 模板语法(附带笔记文档)
"14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
[designmode] adapter pattern
Laser slam learning record
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
FFMPEG关键结构体——AVCodecContext
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
Cloudcompare & PCL point cloud randomly adds noise
QT -- thread
DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
MySQL functions
7.5模拟赛总结