温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

golang热更新的技巧

发布时间:2020-03-27 13:40:45 来源:亿速云 阅读:1448 作者:小新 栏目:编程语言

今天小编给大家分享的是golang热更新的技巧,很多人都不太了解,今天小编为了让大家更加了解golang,所以给大家总结了以下内容,一起往下看吧。一定会有所收获的哦。

golang热更新的技巧

golang可以热更新。

下面我们就来看一下使用go+realize实现热更新:

安装realize

因为之前项目里面安装了 gin 作为 web 框架,所以路由都已经写好了,供后面测试的时候用。

go get github.com/oxequa/realize

安装的时候可能会提示安装某个包失败(golang.org开头),不用慌,这几个包可以去 golang 的仓库克隆下来,放到 $GOPATH/src/golang.org/x 目录下。

配置

运行 realize init 进行一步步配置,也可以一直回车,然后参考我的配置再修改下即可,schema 下的 name 和 path 请按照实际情况写。如果你的机器是 Mac,请把杀进程命令换成pkill,这一步很重要,否则重新编译时 Gin 会一直这样提示,导致热更新失败。

[GIN-debug] [ERROR] listen tcp :8080: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted
settings:
  files:
    outputs:
      status: false
      path: ""
      name: .r.outputs.log
    logs:
      status: false
      path: ""
      name: .r.logs.log
    errors:
      status: true
      path: ""
      name: .r.errors.log
  legacy:
    force: false
    interval: 100ms
server:
  status: true
  open: false
  port: 8080
  host: http://localhost
schema:
- name: gin_app
  path: E:/WorkPlace/Go/app/gin_app/
  commands:
    # clean:
    #   status: true
    # vet:
    #   status: true
    # fmt:
    #   status: true
    # test:
    #   status: true
    # generate:
    #   status: true
    install:
      status: true
    # build:
    #   status: true
    run:
      status: true
  watcher:
    extensions:
    - go
    paths:
    - /
    scripts:
    - type: before
      command: tskill gin_app
      output: true
    ignored_paths:
    - .git
    - .realize
    - vendor

修改realize包

依次执行下面命令

cd /go/src/github.com/oxequa/realize
git fetch
git checkout v2.0.2
go get github.com/oxequa/realize

再去项目目录下运行 realize start 就可以了,热更新也正常了。

关于golang热更新的技巧就分享到这里了,当然并不止以上和大家分析的办法,不过小编可以保证其准确性是绝对没问题的。希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI