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)
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