Golang中是否有一个命令行工具只检查我的源代码的语法?

Gre*_*een 12 go

例如-c,Ruby中有一个选项可以在运行代码之前检查语法:

C:\>ruby --help
Usage: ruby [switches] [--] [programfile] [arguments]
-c              check syntax only

C:\>ruby -c C:\foo\ruby\my_source_code.rb
Syntax OK
Run Code Online (Sandbox Code Playgroud)

Go中是否有类似的功能?

PS Ruby的一个例子只是因为我在Ruby中知道它.不是因为拖钓或其他东西.

小智 24

您可以使用它gofmt来检查语法错误,而无需实际构建项目.

gofmt -e my_file.go > /dev/null
Run Code Online (Sandbox Code Playgroud)

该-e选项定义为:

报告所有错误(不仅仅是不同行的前10个)


gofmt --help

usage: gofmt [flags] [path ...]
  -comments=true: print comments
  -cpuprofile="": write cpu profile to this file
  -d=false: display diffs instead of rewriting files
  -e=false: report all errors (not just the first 10 on different lines)
  -l=false: list files whose formatting differs from gofmt's
  -r="": rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')
  -s=false: simplify code
  -tabs=true: indent with tabs
  -tabwidth=8: tab width
  -w=false: write result to (source) file instead of stdout

  • 好的,这确实在第二次检查时确实有效,只需要将stdout重定向到/ dev / null即可,例如`gofmt -e file.go> / dev / null (2认同)