零号智能

  • Python
  • 部署
  • go
  • VPS
  • 运维
  • 生活琐事
  • html
零号智能
Fantasia's blog npc0.com.
  1. 首页
  2. go
  3. 正文

指针函数解决 golang 的 import cycle not allowed,无需建新文件

2020年9月18日 13099点热度 9人点赞 4条评论

本方案代码只为演示而写,不要喷我干嘛不直接在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()

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2021年5月22日

vpc

这个人很懒,什么都没留下

点赞
下一篇 >

文章评论

  • 玄冥

    OωO 我怎么看不懂我写的啥~

    2020年11月25日
    回复
  • filmi full izle

    Muchos Gracias for your blog. Thanks Again. Much obliged. Korie Dana Shum

    2021年1月18日
    回复
  • full hd tek parça izle

    Nice answer back in return of this query with solid arguments and explaining everything about that. Mavra Penn Minnie

    2021年1月18日
    回复
  • filmi full izle

    I am genuinely thankful to the owner of this site who has shared this fantastic article at at this time. Michaela Shane Dulcia

    2021年1月19日
    回复
  • razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
    回复 filmi full izle 取消回复

    COPYRIGHT © 2021 npc0.com. ALL RIGHTS RESERVED.

    Theme Kratos Made By Seaton Jiang

    粤ICP备19157245号-1

    粤公网安备 44030702002746号