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

huangapple go评论82阅读模式
英文:

Is there a command line tool in Golang to only check syntax of my source code?

问题

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

C:\>ruby --help
用法:ruby [开关] [--] [程序文件] [参数]
-c              仅检查语法

C:\>ruby -c C:\foo\ruby\my_source_code.rb
语法正确

Go语言中是否有类似的功能?

附注:我只是以Ruby为例,因为我在Ruby中了解到这个功能,而不是因为恶作剧或其他原因。

英文:

For example there is a -c option in Ruby that checks syntax before running a code:

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

C:\>ruby -c C:\foo\ruby\my_source_code.rb
Syntax OK

Is there a similar functionality in Go?

P.S. An example from Ruby is only because I know it in Ruby. Not because of trolling or something.

答案1

得分: 31

你可以使用gofmt来检查语法错误,而不需要实际构建项目。

gofmt -e my_file.go > /dev/null

之后你可以使用$? bash变量,返回值0表示成功,2表示语法检查。/dev/null会吞掉代码,但错误会输出到stderr。

-e选项被定义为:

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


gofmt --help

用法:gofmt [flags] [path ...]
  -comments=true:打印注释
  -cpuprofile="":将CPU配置文件写入此文件
  -d=false:显示差异而不是重写文件
  -e=false:报告所有错误(而不仅仅是不同行上的前10个错误)
  -l=false:列出格式与gofmt不同的文件
  -r="":重写规则(例如,'a[b:len(a)] -> a[b:]')
  -s=false:简化代码
  -tabs=true:使用制表符缩进
  -tabwidth=8:制表符宽度
  -w=false:将结果写入(源)文件而不是标准输出
英文:

You can use gofmt to check for syntax errors without actually building the project.

gofmt -e my_file.go > /dev/null

You can later use $? bash variable, return code 0 implies success, 2 means syntax check. /dev/null will eat the code, but the errors go to stderr

The -e option is defined as:

>report all errors (not just the first 10 on different lines)


<pre>
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
</pre>

答案2

得分: 3

只检查语法有多大意义呢?Go编译器非常快,你可以同时编译所有内容。

从这个意义上讲,底层的思维模式与Ruby的思维模式相当不同。

只需使用go buildgo install。http://golang.org/cmd/go/

英文:

Is there much point only checking the syntax? The Go compiler is so fast you might as well compile everything too.

In this sense, the underlying mental model is quite different from that of Ruby.

Just use go build or go install. http://golang.org/cmd/go/

答案3

得分: 2

Ruby是一种解释性语言,因此检查语法的命令可能是有意义的(因为我假设即使在某个地方存在语法错误,您也可能运行该程序)。

另一方面,Go是一种编译语言,因此如果存在语法错误,则根本无法运行。因此,最简单的方法是使用go build构建程序来检查是否存在错误。

英文:

Ruby is an interpreted language so a command that checks the syntax might make sense (since I assume you could potentially run the program even if there are syntax errors at some point).

Go on the other hand is a compiled language so it cannot be run at all if there are syntax errors. As such, the simplest way to know if there are errors is to build the program with go build.

答案4

得分: 1

与@Rick-777一致,我强烈建议使用go build。它执行了go fmt没有的其他检查(例如:缺少或不必要的导入)。

如果你担心在源目录中创建一个二进制文件,你可以使用go build -o /dev/null来丢弃输出,这实际上将go build简化为一个测试代码是否能够构建的过程。这样可以进行语法检查等操作。

编辑:请注意,当构建非主要包时,go build不会生成二进制文件,因此不需要使用-o选项。

英文:

In agreement with @Rick-777, I would strongly recommend using go build. It performs additional checks that go fmt does not (ex: missing or unnecessary imports).

If you are worried about creating a binary in your source directory, you could always go build -o /dev/null to discard the output, which essentially reduces go build to a test that the code will build. That gets you syntax checking, among other things.

EDIT: note that go build doesn't generate a binary when building non-main packages, so you don't need the -o option.

答案5

得分: 0

你可以用gotype替换go build来仅获取go编译器的前端,它会验证语法和结构:
https://godoc.org/golang.org/x/tools/cmd/gotype

它的速度与gofmt相当,但会返回go build中的所有错误。

唯一的注意事项是它似乎需要其他包被go install,否则它找不到它们。不确定为什么会这样。

英文:

Updated answer for those that don't want to settle for only checking with gofmt:

You can substitute gotype for go build to get just the front-end of the go compiler that validates both syntax and structure:
https://godoc.org/golang.org/x/tools/cmd/gotype

It's comparative in speed to gofmt, but returns all the errors you'd get from go build.

The one caveat is that it appears to require other packages to be go installed, otherwise it doesn't find them. Not sure why this is.

答案6

得分: 0

golang语法检查器

将下面的代码放在一个名为gochk的文件中,并将其放在bin目录中,并使用chmod 0755命令进行权限设置。

然后运行gochk --help

#!/bin/bash
#
# gochk v1.0 2017-03-15 - golang语法检查器 - ekerner@ekerner.com
# 查看 --help

# 用法和版本
if \
    test "$1" = "-?" || \
    test "$1" = "-h" || \
    test "$1" = "--help" || \
    test "$1" = "-v" || \
    test "$1" = "--version"
then
    echo "gochk v1.0 2017-03-15 - golang语法检查器 - ekerner@ekerner.com"; echo
    echo "用法:"
    echo "  $0 -?|-h|--help|-v|--version # 显示帮助信息"
    echo "  $0 [ file1.go [ file2.go . . . ] ] # 语法检查"
    echo "如果没有传递参数,则会检查*.go文件"; echo
    echo "示例:"
    echo "  $0 --help # 显示帮助信息"
    echo "  $0 # 语法检查*.go文件"
    echo "  $0 cmd/my-app/main.go handlers/*.go # 语法检查列表"; echo
    echo "作者:"
    echo "  http://stackoverflow.com/users/233060/ekerner"
    echo "  http://stackoverflow.com/users/2437417/crazy-train"
    exit
fi

# 默认为当前目录中的.go文件
gos=$@
if test $# -eq 0; then
    gos=$(ls -1 *.go 2>/dev/null)
    if test ${#gos[@]} -eq 0; then
        exit
    fi
fi

# 使用gofmt测试每个文件
# 感谢Crazy Train在
# http://stackoverflow.com/questions/16863014/is-there-a-command-line-tool-in-golang-to-only-check-syntax-of-my-source-code
#
for go in $gos; do
    gofmt -e "$go" >/dev/null
done
英文:

golang syntax checker

Place the below code in a file called gochk in a bin dir and chmod 0755.

Then run gochk -- help

#!/bin/bash
#
# gochk v1.0 2017-03-15 - golang syntax checker - ekerner@ekerner.com
# see --help

# usage and version
if \
    test &quot;$1&quot; = &quot;-?&quot; || \
    test &quot;$1&quot; = &quot;-h&quot; || \
    test &quot;$1&quot; = &quot;--help&quot; || \
    test &quot;$1&quot; = &quot;-v&quot; || \
    test &quot;$1&quot; = &quot;--version&quot;
then
    echo &quot;gochk v1.0 2017-03-15 - golang syntax checker - ekerner@ekerner.com&quot;; echo
    echo &quot;Usage:&quot;
    echo &quot;  $0 -?|-h|--help|-v|--version # show this&quot;
    echo &quot;  $0 [ file1.go [ file2.go . . . ] ] # syntax check&quot;
    echo &quot;If no args passed then *.go will be checked&quot;; echo
    echo &quot;Examples:&quot;
    echo &quot;  $0 --help # show this&quot;
    echo &quot;  $0 # syntax check *.go&quot;
    echo &quot;  $0 cmd/my-app/main.go handlers/*.go # syntax check list&quot;; echo
    echo &quot;Authors:&quot;
    echo &quot;  http://stackoverflow.com/users/233060/ekerner&quot;
    echo &quot;  http://stackoverflow.com/users/2437417/crazy-train&quot;
    exit
fi

# default to .go files in cwd
gos=$@
if test $# -eq 0; then
    gos=$(ls -1 *.go 2&gt;/dev/null)
    if test ${#gos[@]} -eq 0; then
        exit
    fi
fi

# test each one using gofmt
# credit to Crazy Train at
# http://stackoverflow.com/questions/16863014/is-there-a-command-line-tool-in-golang-to-only-check-syntax-of-my-source-code
#
for go in $gos; do
    gofmt -e &quot;$go&quot; &gt;/dev/null
done

huangapple
  • 本文由 发表于 2013年6月1日 01:46:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/16863014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定