Difference between go build and go build file.go

1,186 views
Skip to first unread message

larry.ba...@gmail.com

unread,
Oct 7, 2013, 4:54:23 PM10/7/13
to golan...@googlegroups.com
What's the difference between `go build` and `go build file.go`?

I'm asking because when I run `go build` on a package that imports a local package, then I get this error message.

`can't load package: C:\go\src\bug\main.go:3:8: local import "./local_file" in non-local package`

However, when I specify a file name it works. Ex `go build main.go`

Console history on Windows XP.

C:\gopath\src\bug:>go version
go version go1.1 windows/386
C:\gopath\src\bug:>dir
...
<DIR>          local_file
55 main.go
...

C:\gopath\src\bug:>type main.go
package main

import _ "./local_file"

func main() {
}

C:\gopath\src\bug:>type local_file\local_file.go
package local_file

import "fmt"

func init() {
fmt.Println("Called: local_file.init()")
}

C:\gopath\src\bug:>go run main.go
Called: local_file.init()

C:\gopath\src\bug:>go build main.go

C:\gopath\src\bug:>dir
...
<DIR>          local_file
1,285,120 main.exe
55 main.go
...

C:\gopath\src\bug:>go build
can't load package: C:\gopath\src\bug\main.go:3:8: local import "./local_file" in non-local package

James Bardin

unread,
Oct 7, 2013, 6:29:36 PM10/7/13
to golan...@googlegroups.com, larry.ba...@gmail.com


On Monday, October 7, 2013 4:54:23 PM UTC-4, larry.ba...@gmail.com wrote:
What's the difference between `go build` and `go build file.go`?


import _ "./local_file"



Relative imports are discouraged, and can't be automatically discovered by the build tool outside of your GOPATH. 
Create a package with a proper import path in your GOPATH. Yes, there may be ways around it, but it's only going to continue to cause you trouble.


larry.ba...@gmail.com

unread,
Oct 7, 2013, 6:36:39 PM10/7/13
to golan...@googlegroups.com, larry.ba...@gmail.com

Please explain this part more " can't be automatically discovered by the build tool outside of your GOPATH."
I'm confused because the source files are all located within the gopath.

Also what is `go build` doing that `go build main.go` isn't?

Jesse McNelis

unread,
Oct 7, 2013, 7:00:25 PM10/7/13
to larry.ba...@gmail.com, golang-nuts
On Tue, Oct 8, 2013 at 9:36 AM, <larry.ba...@gmail.com> wrote:

Please explain this part more " can't be automatically discovered by the build tool outside of your GOPATH."
I'm confused because the source files are all located within the gopath.

Also what is `go build` doing that `go build main.go` isn't?

Local imports don't work for packages within your $GOPATH.
Use the full path relative to  $GOPATH/src/


--
=====================
http://jessta.id.au

larry.ba...@gmail.com

unread,
Oct 7, 2013, 7:12:29 PM10/7/13
to golan...@googlegroups.com, larry.ba...@gmail.com, jes...@jessta.id.au
Thank you for the links.
This might be going off topic a little bit but after reading the link it states this.

To avoid ambiguity, Go programs cannot use relative import paths within a work space.
If that's true, then why does `go build main.go` and `go run main.go` work? Shouldn't those commands be throwing the same error message as `go build`.

Carlos Castillo

unread,
Oct 7, 2013, 8:15:16 PM10/7/13
to golan...@googlegroups.com, larry.ba...@gmail.com, jes...@jessta.id.au
go build file.go (even if file.go is in GOPATH) isn't building a package in a workspace, so it follows doesn't follow that rule. The downside is that you have to name all the *.go files, you don't get some of the automatic behaviour of the go command (eg: testing & installing), and you can't build a package (which requires installing), only executables. Also, it's not go-gettable.

The "go build file.go" should only ever be used for simple one source file binaries that you don't intend to ever distribute or re-use code from. Even then, it's often simpler and faster to follow the go tool's conventions:
  • one package per folder (and vice versa)
  • no relative imports (ever)
"go run file.go" follows the same logic, and is even less recommended.

Both patterns are meant for very specific uses, and should be avoided in regular programs and packages.

Dave Cheney

unread,
Oct 7, 2013, 8:34:49 PM10/7/13
to Carlos Castillo, golang-nuts, larry.ba...@gmail.com, Jesse McNelis
+1.

In essence go run somefile.go is the equivalent of go build -work
somefile.go except the former puts in binary in a easier to predict
location.

As Carlos said, these are exceptions to the normal package building
logic and should not be interpreted as anything more than that.

Cheers

Dave
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

larry.ba...@gmail.com

unread,
Oct 8, 2013, 1:56:22 AM10/8/13
to golan...@googlegroups.com, larry.ba...@gmail.com
Thanks everyone. I think I understand now.

Volker Dobler

unread,
Oct 8, 2013, 3:01:57 AM10/8/13
to golan...@googlegroups.com, larry.ba...@gmail.com
Am Dienstag, 8. Oktober 2013 02:15:16 UTC+2 schrieb Carlos Castillo:
The "go build file.go" should only ever be used for simple one source file binaries that you don't intend to ever distribute or re-use code from. Even then, it's often simpler and faster to follow the go tool's conventions: [...]
"go run file.go" follows the same logic, and is even less recommended.


There is a legitimate uses for "go run file.go" in "normal packages": Code generation.
Suppose your package needs large tables of fixed data (e.g. Unicode tables)
and you generate them from some online database. This is often done
by "go run gen.go > table-go; go fmt table.go" with gen.go a package main
which generates the fixed tables.

For anything else: "go run file.go" is your local version of play.golang.org.

V.

Reply all
Reply to author
Forward
0 new messages