Giter VIP home page Giter VIP logo

Comments (3)

edwingeng avatar edwingeng commented on May 14, 2024

这例子不完整,有太多可能,建议提供完整可运行的例子

from hotswap.

libaichuan0707 avatar libaichuan0707 commented on May 14, 2024
//------------------注册相关----------------
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.

edwingeng avatar edwingeng commented on May 14, 2024

从结果上看,getICase创建了一个类似C++虚指针的东西,这用法已经走到golang边缘地带了

from hotswap.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.