convert old makefile for Go1

236 views
Skip to first unread message

dlin

unread,
Feb 20, 2012, 1:53:58 AM2/20/12
to golan...@googlegroups.com
I'm trying to convert my old Makefile for Go1.
For simple case, I could build my foo.go by

foo : foo.go
  go build -o $@ $<

But, for two files in same directory, what's the proper method?

./file.go # package file
./hello.go # package main  use import "./file"

hello : hello.go file.go
  go build -o $@ $+    # this method won't work.

The error message will like:

: /home/dlin/prj/golang/samples/.: found packages main (hello.go) and file (file.go)

Dustin

unread,
Feb 20, 2012, 2:00:05 AM2/20/12
to golan...@googlegroups.com
Just don't have a makefile and type "go build"

All of the files in the directory should be in the same package.

dlin

unread,
Feb 20, 2012, 2:34:55 AM2/20/12
to golan...@googlegroups.com
There are other files which I should use makefile to get them work.

So, what's my hello.go & file.go should be arranged?

Could I do as

hello.go
file/file.go

GOPATH=$PWD go build

I've tried, and got errors as:

$find .                        
./hello.go
./file/file.go

$ go build 
can't load package: .: path "/home/dlin/prj/golang/samples/x" not inside a GOPATH

$GOPATH=$PWD  go build                  
can't load package: .: path "/home/dlin/prj/golang/samples/x" not inside a GOPATH

Daniel Lin

unread,
Feb 20, 2012, 2:45:06 AM2/20/12
to Dustin, golan...@googlegroups.com
I require the file.go as package "file".
That's a sample for package file.

Dustin

unread,
Feb 20, 2012, 2:50:22 AM2/20/12
to golan...@googlegroups.com, Dustin

On Sunday, February 19, 2012 11:45:06 PM UTC-8, dlin wrote:
I require the file.go as package "file".
That's a sample for package file.

  You must do one of the following:

1) file.go in a separate directory and import it the "normal" way.
2) Just change the package in file.go to the same package hello.go declares.

Sanjay Menakuru

unread,
Feb 20, 2012, 2:51:00 AM2/20/12
to golan...@googlegroups.com
You should almost never do GOPATH=$PWD, as it is unlikely to do what you want. Source code for a package is looked for in <GOPATH-element>/src/ not <GOPATH-element>/.

I'll give a quick example of how you could setup your environment. I'm going to assume you want 1 package named util and 1 command, named hello that depends on util. (I am not using a package named "file" to avoid confusion. This should give you the idea)

In .bashrc:
export GOPATH=~/go/external/:~/go/mycode/

Note: The reason for having 2 entries is so you can keep your code separate from code downloaded by go get (go get will automatically use the first entry in GOPATH)

In ~/go/mycode/src/util, create a file called "util.go", containing your code for the "util" package
In ~/go/mycode/src/hello, create a file hello.go and put your code for your "hello" command in there. Make sure that you give it package main as you want it to be executable not a package. You are free to import "util" in this file. 

To {build|test|install} your util package, simply type "go {build|test|install} util". To {build|test|install} your hello command simply type "go {build|test|install} hello". 

Note that putting your source directly under the root of <GOPATH-elem>/src/ is probably not best practice. You should probably put it under (in increasing order of preferance) <GOPATH-elem>/src/<my-project-name> or <GOPATH-elem>/src/<my-company-name>/ or <GOPATH-elem>/src/<my-vcs-path>.

Sanjay Menakuru

unread,
Feb 20, 2012, 3:05:52 AM2/20/12
to golan...@googlegroups.com
This is a more in-depth tutorial http://tip.golang.org/doc/code.html

Daniel Lin

unread,
Feb 20, 2012, 4:25:58 AM2/20/12
to Sanjay Menakuru, golan...@googlegroups.com
Sanjay,

Do you think this is bug?

export GOPATH=~/prj

~/prj/src/util/util.go  #packge util
~/prj/src/hello/hello.go # package main

hello.go import "util".

go build util  # work
(cd ~/prj/src/hello ; go build )# work
(cd ~/prj/src/util ; go build hello )# work
(cd ~/prj/src/hello ; go build hello )# work
(cd ~/prj/src ; go build hello )#  failed

build output "hello" already exists and is a directory

Kyle Lemons

unread,
Feb 20, 2012, 5:01:43 AM2/20/12
to Daniel Lin, Sanjay Menakuru, golan...@googlegroups.com
That's WorkingAsIntended because "build" is primarily designed for $PWD-work and will build the binary into your $PWD, which in your case collides with a directory.  Use "go install" if you want the output to go somewhere predictable.

Marcel van Lohuizen

unread,
Feb 20, 2012, 7:28:05 AM2/20/12
to golang-nuts
It is advisable and more conventional to have the binary and package
in different directories. In some cases, however, for example when
the binary is a tool used to generate data for the package, it may be
nicer to include them in the same directory.

In your case, you could accomplish this by including
// +build ignore
somewhere at the top of hello.go, and then build your package using
go build

and run your binary using
go run hello.go

you could also write a Makefile including only:
hello: hello.go
go build $^
and build hello using
make hello

See src/pkg/unicode/{Makefile | maketables.go} for an example.

Marcel
Reply all
Reply to author
Forward
0 new messages