本方案代码只为演示而写,不要喷我干嘛不直接在get里面调用Stop~~~
robot/app/app.go文件
package main
import (
"fmt"
"robot/web"
)
func StopRobot(uid int) {
fmt.Println("end", uid)
}
func main() {
g := gin.Default()
g.GET("/stop", func(c *gin.Context) {
b.Stop(StopRobot)
}
go g.Run()
}
robot/web/web.go文件
import(
"web"
"robot/app"
)
type appStop func(int)
func Stop(e appStop) {
e(1)
}
如果A文件(app.go)不是直接调用B文件(web.go)里的目标函数,可以写个函数把指针赋给该文件的全局,如有异议请留言。(半路出家玄)
robot/web/web.go文件
var StopRobot appStop
type appStop func(string, bool)
func SetStop(app appStop) {
StopRobot = app
}
func Stop() {
StopRobot(1)
}
robot/app/app.go文件main()可改成如下先声明后调用
web.SetStop(Stop)
web.Stop()
文章评论
OωO 我怎么看不懂我写的啥~
Muchos Gracias for your blog. Thanks Again. Much obliged. Korie Dana Shum
Nice answer back in return of this query with solid arguments and explaining everything about that. Mavra Penn Minnie
I am genuinely thankful to the owner of this site who has shared this fantastic article at at this time. Michaela Shane Dulcia