Is there a command line tool in Golang to only check syntax of my source code?
Solution 1
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/
Solution 2
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
.
Solution 3
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.
Related videos on Youtube
Green
Node.js Angular, React, React-native JavaScript AWS Full Stack Web Developer from Ukraine.
Updated on June 04, 2022Comments
-
Green almost 2 years
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.
-
amon almost 11 yearsThis is factually incorrect. All modern interpreted languages compile to an intermediate representation (bytecode/opcodes) which are then interpreted. These are not some ancient BASIC where each line is parsed every time before it is executed. The
-c
(“compile”) option just prevents the compiled representation from being run, thus behaving like a traditional compiler with output targeted to/dev/null
. -
laurent almost 11 years@amon, I didn't actually write anything about bytecodes or opcodes, was just highlighting the difference between an interpreted and compiled language. In a language like PHP, there can potentially be an
include
statement at any point in the code. Even if there is a syntax error in the included file, the program will run perfectly well until it reaches this file, at which point it will crash. -
laurent almost 11 yearsThis is very different from a compiled language which will be entirely converted to bytecode before being executed. So, there's no need to "check the syntax" of a compiled language - if it builds it's ok, if not there's an error. An interpreted language however can run and still have syntax errors, which is why the extra
-c
flag makes sense for Ruby (since I assume it will check the full source tree, like an actual compiler) but not for compiled languages (which already have a compiler). -
Gustav over 9 yearsMost of the time you don't want to upload, commit, deploy or waste time building something with incorrect syntax. Checking the syntax would be done before doing any of above.
-
Rick-777 over 9 yearsWhy would you want to upload, commit, deploy or waste time building something that hasn't been compiled yet? This is kind-of the point. The
go
tool does syntax checking and compilation very fast so there's nothing to be gained by having "only" a syntax checker. When compilation has succeeded, then upload, commit, deploy etc to your heart's content. -
Gustav over 9 yearsIn some cases you can't compile and test the whole program on your own computer. Think of a CAN-network in a car, not that easy to test on a personal computer.