例如,Ruby 中有一个 -c
选项,可以在运行代码之前检查语法:
C:\>ruby --help
Usage: ruby [switches] [--] [programfile] [arguments]
-c check syntax only
C:\>ruby -c C:\foo\ruby\my_source_code.rb
Syntax OK
Go 中有类似的功能吗?
附: Ruby 的一个例子只是因为我在 Ruby 中知道它。不是因为拖钓什么的。
最佳答案
您可以使用 gofmt
来检查语法错误,而无需实际构建项目。
gofmt -e my_file.go > /dev/null
您以后可以使用 $? bash 变量,返回码 0 表示成功,2 表示语法检查。/dev/null 会吃掉代码,但错误会转到 stderr
-e
选项定义为:
report all errors (not just the first 10 on different lines)
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
关于go - Golang 中是否有命令行工具来仅检查我的源代码的语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16863014/