Precautions

Since Go does not support hot compilation features, every code change requires manually stopping, compiling, and running the code files. The run command does not implement hot compilation but provides an automatic compilation feature. When developers modify the go files in the project, this command will automatically compile the current program, stop the existing one, and run the new version.

Auto Compiling - 图1tip

The run command will recursively monitor all go file changes in the current working directory to implement automatic compilation.

Usage Help

  1. $ gf run -h
  2. USAGE
  3. gf run FILE [OPTION]
  4. ARGUMENT
  5. FILE building file path.
  6. OPTION
  7. -p, --path output directory path for built binary file. it's "./" in default
  8. -e, --extra the same options as "go run"/"go build" except some options as follows defined
  9. -a, --args custom arguments for your process
  10. -w, --watchPaths watch additional paths for live reload, separated by ",". i.e. "manifest/config/*.yaml"
  11. -h, --help more information about this command
  12. EXAMPLE
  13. gf run main.go
  14. gf run main.go --args "server -p 8080"
  15. gf run main.go -mod=vendor
  16. gf run main.go -w "manifest/config/*.yaml"
  17. DESCRIPTION
  18. The "run" command is used for running go codes with hot-compiled-like feature,
  19. which compiles and runs the go codes asynchronously when codes change.

Example of configuration file format:

  1. gfcli:
  2. run:
  3. path: "./bin"
  4. extra: ""
  5. args: "all"
  6. watchPaths:
  7. - api/*.go
  8. - internal/controller/*.go

Parameter introduction:

NameDefaultMeaningExample
path./Specifies the directory to store the compiled binary files.
extraSpecifies the command arguments for underlying go build
argsSpecifies the command line arguments for running the binary files
watchPathSpecifies the file path format for local project file monitoring. Multiple paths can be separated by ,. This parameter’s format is the same as the filepath.Match method parameter in the standard libraryinternal/*.go

Usage Example

Generally, gf run main.go is sufficient

  1. $ gf run main.go --swagger
  2. 2020-12-31 00:40:16.948 build: main.go
  3. 2020-12-31 00:40:16.994 producing swagger files...
  4. 2020-12-31 00:40:17.145 done!
  5. 2020-12-31 00:40:17.216 gf pack swagger packed/swagger.go -n packed -y
  6. 2020-12-31 00:40:17.279 done!
  7. 2020-12-31 00:40:17.282 go build -o bin/main main.go
  8. 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
  9. 2020-12-31 00:40:18.696 build: main.go
  10. 2020-12-31 00:40:18.775 producing swagger files...
  11. 2020-12-31 00:40:18.911 done!
  12. 2020-12-31 00:40:19.045 gf pack swagger packed/swagger.go -n packed -y
  13. 2020-12-31 00:40:19.136 done!
  14. 2020-12-31 00:40:19.144 go build -o bin/main main.go
  15. 2020-12-31 00:40:21.367 bin/main
  16. 2020-12-31 00:40:21.372 build running pid: 40954
  17. 2020-12-31 00:40:21.437 [DEBU] [ghttp] SetServerRoot path: /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf-demos/public
  18. 2020-12-31 00:40:21.440 40954: http server started listening on [:8199]
  19. ...

Common Issues

too many open files on macOS