当前位置:网站首页>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)))
}

边栏推荐
- Make a short video clip number of we media film and television. Where can I download the material?
- 7.5模拟赛总结
- Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
- 【luogu P3295】萌萌哒(并查集)(倍增)
- What are Yunna's fixed asset management systems?
- 2022.7.5-----leetcode. seven hundred and twenty-nine
- 时区的区别及go语言的time库
- 传输层协议------UDP协议
- C # input how many cards are there in each of the four colors.
- 18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
猜你喜欢

MySql——CRUD

AtCoder Beginner Contest 254【VP记录】

数据库遇到的问题

FFT learning notes (I think it is detailed)

Transport layer protocol ----- UDP protocol

Hudi of data Lake (1): introduction to Hudi

GD32F4xx uIP协议栈移植记录

Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4

FFMPEG关键结构体——AVFormatContext

Browser local storage
随机推荐
QT -- thread
Open3D 点云随机添加噪声
[designmode] Decorator Pattern
DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
如何解决ecology9.0执行导入流程流程产生的问题
Wechat applet -- wxml template syntax (with notes)
AtCoder Beginner Contest 258【比赛记录】
云呐|固定资产管理系统主要操作流程有哪些
How much do you know about the bank deposit business that software test engineers must know?
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
关于slmgr命令的那些事
NSSA area where OSPF is configured for Huawei equipment
【QT】Qt使用QJson生成json文件并保存
Single merchant v4.4 has the same original intention and strength!
Global and Chinese market of digital serial inverter 2022-2028: Research Report on technology, participants, trends, market size and share
Huawei equipment is configured with OSPF and BFD linkage
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
Add noise randomly to open3d point cloud
OS i/o devices and device controllers