On Fri, 7 Nov 2014 04:34:57 -0800 (PST)
anjan bacchu <
anjan....@gmail.com> wrote:
> I'm reviewing a book called "Go in action" from manning and
> learning go lang at the same time and found this issue(?).
> I have a very simple app. When I "build" and then run the code, go
> doesn't complain. However, if I just try to run the code, go
> complains.
>
> What am I missing ?
This is by design. Please use the
go help {command}
to get quick help on particular commands.
From there (Go 1.3.2):
$ go help run
usage: go run [build flags] [-exec xprog] gofiles... [arguments...]
Run compiles and runs the main package comprising the named Go source
files. A Go source file is defined to be a file ending in a literal
".go" suffix.
...
$ go help build
usage: go build [-o output] [-i] [build flags] [packages]
Build compiles the packages named by the import paths,
along with their dependencies, but it does not install the results.
...
As you can see, `go run` expects a mandatory list of source code files,
all of which are supposed to comprise the package "main" ("meta"
package representing the "top-level" code and data of an executable)
while `go build` works on a much higher level and operates on a set of
packages, not files. This one is the chief reason of why it does not
run the produced executable: there may be many of them. For instance,
you can call `go build ./...` to build the whole hierarchy of packages
rooted in the current directory.
See also this recent thread [1] which touches on basically the same
issues and contains well put explanations from experienced folks.
1.
https://groups.google.com/d/topic/golang-nuts/50wO63bZ7rc/discussion