go - Golang 中是否有命令行工具来仅检查我的源代码的语法?

标签 go

例如,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/

相关文章:

go - 优雅地关闭 Gorilla 服务器

go - 高并发下Go `net/http`的一些困惑

go - 将 JSON 响应传递到 Go 模板

json - 在golang中使用未知结构的json对象有没有更简洁的方法?

go - 在 golang 中有效地包装公共(public) sdk 类型

go - 每次请求后关闭 http2 客户端连接

go - 如何创建一个新的GoClipse项目,使其遵循正常的Go项目结构?

Go相当于Python的字典

go - 如何使用 Go 将 float 格式化为字符串

go - 在没有明确定义的结构的情况下插入 BigQuery