Comments (3)
这例子不完整,有太多可能,建议提供完整可运行的例子
from hotswap.
//------------------注册相关----------------
var allModule = make(map[string]func())
func RegModule(name string, f func()){
allModule[name] = f
}
func GetModule(name string) func() {
return allModule[name]
}
//------------------注册相关----------------
type ITestModule interface {
PackModule()
}
//---------------原始的struct(也就是需要热更的部分)--------------
type HCase1 struct {}
func (h *HCase1) PackModule() {
fmt.Println("case1")
}
func NewHCase1() ITestModule {
return &HCase1{}
}
//---------------原始的struct--------------
//---------------新的struct(也就是需要热更的部分)--------------
type HCase2 struct {}
func (h *HCase2) PackModule() {
fmt.Println("case2")
}
func NewHCase2() ITestModule {
return &HCase2{}
}
//---------------新的struct--------------
type UseCase struct {
ITestModule
}
func NewUseCase(createApi func() ITestModule) *UseCase {
uc := &UseCase{}
uc.CreateHotAPI(createApi)
return uc
}
func (uc *UseCase) DepInjectTest1(name string) {
RegModule(name, uc.PackModule)
}
func (uc *UseCase) DepInjectTest2(name string) {
RegModule(name, getICase(uc).PackModule)
}
func getICase(iuc interface{}) ITestModule {
return iuc.(ITestModule)
}
func (uc *UseCase) CreateHotAPI(createApi func() ITestModule) error {
api := createApi()
uc.ITestModule = api
return nil
}
func test1() {
fmt.Println("------------test1-------------")
uc := NewUseCase(NewHCase1)
uc.DepInjectTest1("name")
GetModule("name")()
uc.CreateHotAPI(NewHCase2)
GetModule("name")()
fmt.Println("------------test1-------------\n\n")
}
func test2() {
fmt.Println("------------test2-------------")
uc := NewUseCase(NewHCase1)
uc.DepInjectTest2("name")
GetModule("name")()
uc.CreateHotAPI(NewHCase2)
GetModule("name")()
fmt.Println("------------test2-------------\n\n")
}
func main() {
//test1和test2相差的地方是DepInjectTest1和DepInjectTest2
test1()
test2()
}
看起来跟热更本身没啥关系, 我直接写弄个新的struct替换接口,如果RegModule调用的方式不一样, 结果也是不一样的
from hotswap.
从结果上看,getICase创建了一个类似C++虚指针的东西,这用法已经走到golang边缘地带了
from hotswap.
Related Issues (8)
- 请问有交流群吗? HOT 1
- which platforms are supported? HOT 1
- 同学,您这个项目引入了100个开源组件,存在4个漏洞,辛苦升级一下
- 提问: 依赖问题 HOT 1
- 大佬介绍一下原理,以及cgo需要主程序开启吗,性能怎么优化 HOT 1
- Does not work on ARM HOT 2
- 如何解决调试问题呢? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hotswap.