注意事项

由于Go是不支持热编译特性的,每一次代码变更后都要重新手动停止、编译、运行代码文件。run命令也不是实现热编译功能,而是提供了自动编译功能,当开发者修改了项目中的go文件时,该命令将会自动编译当前程序,并停止原有程序,运行新版的程序。

run命令会递归监控当前运行目录的所有go文件变化来实现自动编译。

使用帮助

$ gf run -h
USAGE
    gf run FILE [OPTION]

ARGUMENT
    FILE    building file path.

OPTION
    -p, --path         output directory path for built binary file. it's "./" in default
    -e, --extra        the same options as "go run"/"go build" except some options as follows defined
    -a, --args         custom arguments for your process
    -w, --watchPaths   watch additional paths for live reload, separated by ",". i.e. "manifest/config/*.yaml"
    -h, --help         more information about this command

EXAMPLE
    gf run main.go
    gf run main.go --args "server -p 8080"
    gf run main.go -mod=vendor
    gf run main.go -w "manifest/config/*.yaml"

DESCRIPTION
    The "run" command is used for running go codes with hot-compiled-like feature,
    which compiles and runs the go codes asynchronously when codes change.

配置文件格式示例:

gfcli:
  run:
    path:  "./bin"
    extra: ""
    args:  "all"
    watchPaths:
    - api/*.go
    - internal/controller/*.go

参数介绍:

名称默认值含义示例
path

./

指定编译后生成的二进制文件存放目录。
extra
指定用于底层go build的命令参数
args
指定启动运行二进制文件的命令行参数
watchPath
指定本地项目文件监听的文件路径格式,支持多个路径使用,覆盖分隔。该参数的格式同标准库的filepath.Match方法参数

internal/*.go

使用示例

一般gf run main.go即可

$ gf run main.go --swagger
2020-12-31 00:40:16.948 build: main.go
2020-12-31 00:40:16.994 producing swagger files...
2020-12-31 00:40:17.145 done!
2020-12-31 00:40:17.216 gf pack swagger packed/swagger.go -n packed -y
2020-12-31 00:40:17.279 done!
2020-12-31 00:40:17.282 go build -o bin/main  main.go
2020-12-31 00:40:18.696 go file changes: "/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf-demos/packed/swagger.go": WRITE
2020-12-31 00:40:18.696 build: main.go
2020-12-31 00:40:18.775 producing swagger files...
2020-12-31 00:40:18.911 done!
2020-12-31 00:40:19.045 gf pack swagger packed/swagger.go -n packed -y
2020-12-31 00:40:19.136 done!
2020-12-31 00:40:19.144 go build -o bin/main  main.go
2020-12-31 00:40:21.367 bin/main 
2020-12-31 00:40:21.372 build running pid: 40954
2020-12-31 00:40:21.437 [DEBU] [ghttp] SetServerRoot path: /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf-demos/public
2020-12-31 00:40:21.440 40954: http server started listening on [:8199]
...


常见问题

too many open files on macOS




Content Menu

  • No labels

11 Comments

  1. 是否考虑也把config/*.toml 文件的更改,也加入到热更新里面去?  源码似乎只针对go文件进行了热更新。

    1. 配置文件默认有热更新特性:配置管理

  2. PS D:\go-project\autoUpdate> gf run main.go
    2021-09-04 19:24:32.556 build: main.go
    2021-09-04 19:24:32.569 go build -o bin\main.exe  main.go
    2021-09-04 19:24:32.571 build error:
    exec: "D:\\go-project\\autoUpdate": file does not exist
    PS D:\go-project\autoUpdate> gf run main.go -mod=mod
    2021-09-04 19:25:04.416 build: main.go
    2021-09-04 19:25:04.428 go build -o bin\main.exe -mod=mod main.go
    2021-09-04 19:25:04.431 build error:
    exec: "D:\\go-project\\autoUpdate": file does not exist

    无法进行编译是怎么回事   直接使用build 也不可以  但是直接使用go build 就没有问题

  3. 郭强 海亮 

    ➜  gf run .\main.go
    build: .\main.go
    go build -o ./\main.exe  .\main.go
    go file changes: "E:\\go-workspace\\MyGoFrame\\myapp\\main.go": WRITE
    build: .\main.go
    go build -o ./\main.exe  .\main.go
    ./\main.exe
    ./\main.exe
    build running pid: 16744
    build running pid: 9008
    2022-04-22022-024 -029:2 09:3322::4477.2.3223 1 [DE[BUD]EBU ]Set SSeervterSRoeortv peatrh:R oE:o\gto- wporakstpahce:\ MyEG:oF\rgaome-\mwyaoprpk\srpesaocurece\\MpuybGloic F
    rame\myapp\resource\public
    2022-04-22 09:322:4022-04-22 079.:23525: 47.[2D5E7B U][D EsBwUa]gge rs wuaig giesr  sueir viisn gs eartv iandgd raets sa:d dhrtetsps::/ /h1t2t7p.:0/./01.217:.801.909./1s:w8a1g9g9e/rs/w
     a
    gge2r0/2 2
    -0240-2222- 0049-:2322 :0497:.3226:44 7.2[I6N7F O][I oNpFeOn]api  osppeencaipfii csapteicoinf iicsa tsieornv iinsg  saetr vaidndgr eat addsrsess: :http:/ /h1t2t7p.:0/./01.217:.801.909.
    /1a:p8i1.9j9s/oanp i
    .json
    20222-00242-04--2222  0099::3322:47.280 :4[IN7FO].2 8p2i d[1[6F7A4T4A]]:  h9tt0p0 8s:e rnveetr. Lsisttaernt efda illiesdt:e nliinsgt eonn  t[c:p8 1:9891]9 9
    : bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
    1. 9008: net.Listen failed
       1).  github.com/gogf/gf/v2/net/ghttp.(*gracefulServer).getNetListener
            E:/go-workspace/pkg/mod/github.com/gogf/gf/v2@v2.0.0-rc3/net/ghttp/ghttp_server_gracef
     u lA.DgDoR:E1S9S2
    |   M E2T)H.O D  g|i t h u b . cRoOmU/TgEo g f / g f / v|2 / n e t / g h t t p . ( * g r a c e f u l S e r v e r ) .HLAiNsDtLeEnRA n d S e r v e
                     E : / g o - w o r k s p a c e /|p k g / m o d / g i t h uMbI.DcDoLmE/WgAoRgEf / g f / v 2 @ v 2 . 0 . 0
    --r-c-3-/-n-e-t-/-g-h|t-t-p-/-g-h-t-t-p|_-s-e--r-ve--r-_-g--ra----ceful-.--g|-o-:-7-2-
    - - - -3-)-.- - -g-i-t-h-u-b-.-c-o-m-/-g-o-g-f-/-g-f-/-v-2-/-n-e-t-/-g--h-t-t-p-.-(--*-S-er-v-e-r-)-.-s-tartServer.-f-u-n-c-1-
    - | - - -       -E-:-/-g-o---w-o-r-k-s-p-a-c-e-/-p-k-g-/m-o-d-/-g-i-t-h-u-b-.-c-o
    m / g:o8g1f9/9g f / v|2 @AvL2L. 0 . 0 -|r c/3*/ n e t / g h t t p / g h t t p|_ segrivtehru.bg.oc:o5m4/9g
    o2g.f /lgifs/tve2n/ ntectp/ g:h8t1t9p9.:i nbtienrdn:a lOMnildyd loenwea ruesSaegrev eorfT reaaccihn gs o|c kGeLtO BaAdLd rMess (prIoDtDoLcEoWlA/RnEe t w o r k   a d d r e  s s /  p
    o-r-t-)- --i-s--- |n-o-r-m-a-l-l-y- |p-e-r-m-i-t-t-e-d-.-
    -S-t-a-c-k-:-
    -1|.- - -g-i-t-h-u-b-.-c-o-m-/-g-o-g-f-/-g-f-/-v-2-/-n-e-t-/---g-h--t-t----p-.-(-*-S-e-r-v-e-r-)-.-s-t-a-r-t-S-e-r-v-e-r-.-f-u-n|c-1-
    - - - - -E-:-/-g-o---w-o-r-k-s-p-a-c-e-/-p-k-g-/-m-o-d-/-g-i-t-h-u
    b . c:o8m1/9g9o g f /|g fA/LvL2 @ v 2 .|0 ./0*- r c 3 / n e t / gh t t p / g h|t tmpy_aspepr/vienrt.egron:a5l5/3s

    出现一些奇怪的词:`[I6N7F O][I oNpFeOn]api osppeencaipfii csapteicoinf iic` 好奇怪??

    1. 稳定复现?

      1. 只出现过一次。后续再次执行`gf run ./main.go` 不再输出太多东西了

        ➜  gf run .\main.go
        build: .\main.go
        go build -o ./\main.exe  .\main.go
        ./\main.exe
        build running pid: 7368
        2022-04-22 09:49:32.464 [DEBU] SetServerRoot path: E:\go-workspace\MyGoFrame\myapp\resource\public
        2022-04-22 09:49:32.483 [DEBU] swagger ui is serving at address: http://127.0.0.1:8199/swagger/
        2022-04-22 09:49:32.489 [INFO] openapi specification is serving at address: http://127.0.0.1:8199/api.json
        ....后续省略...

        我的环境是:

        GoFrame CLI Tool v2.0.6, https://goframe.org
        GoFrame Version: v2.0.0-rc3 in current go.mod
        CLI Installed At: E:\go-workspace\bin\gf.exe
        CLI Built Detail:
          Go Version:  go1.17.7
          GF Version:  v2.0.6
          Git Commit:  2022-03-31 16:57:32 66803fd6641e31c60f0489180668243ea092b87b
          Build Time:  2022-03-31 16:55:38
        1. 那可能只是终端问题

  4. gf  run mian.go 报 add watch failed for path "/home/bugme/Project/goframe/goframe": no space left on device  

    设备空间不足?

  5. gf run 自动编译会发生端口被占用的情况, 请问这样怎么解决啊

  6. 有一个需求,不知道各位有没有:

    我写了一个插件,自动生成业务文件,但是使用gf run 调试时,生成的第一个文件后,进程就挂了,是否有办法通知gf 暂时不要重启,等文件全部生成完成后,再通知它重启进程。

    我现在测试是用 go run 来执行,当然编译后肯定没有这个问题了。

  7. 2.7 watch 不生效啊