splitting package main in to separate files

3,015 views
Skip to first unread message

Sonia Hamilton

unread,
Sep 10, 2012, 6:35:53 AM9/10/12
to golan...@googlegroups.com
I'm wondering how to split package main in to separate files. For example I have this large program which runs nicely:

% cat hello.go 
package main

import "fmt"

func hello()   { fmt.Println("Hello"); return }
func goodbye() { fmt.Println("Goodbye"); return }
func main()    { hello(); goodbye() }

% go run hello.go
Hello
Goodbye

hello.go is getting too large, so I want to split it in to two files, hello.go and goodbye.go:

% cat hello.go 
package main

import "fmt"

func hello() { fmt.Println("Hello"); return }
func main()  { hello(); goodbye() }

% cat goodbye.go 
package main

import "fmt"

func goodbye() { fmt.Println("Goodbye"); return }

But when I run hello.go, I get an error:

% go run hello.go 
# command-line-arguments
./hello.go:6: undefined: goodbye

What am I doing wrong? My understanding (from the Packages chapter of [Summerfield]) is that you can split the main package in to separate files by "making the first statement in each file (excluding comments), package main".

[1] [Summerfield] "Programming in Go", page 408.

--
Sonia Hamilton

Jan Mercl

unread,
Sep 10, 2012, 6:42:52 AM9/10/12
to Sonia Hamilton, golan...@googlegroups.com
On Mon, Sep 10, 2012 at 12:35 PM, Sonia Hamilton
<sonia.s...@gmail.com> wrote:
> % go run hello.go
> # command-line-arguments
> ./hello.go:6: undefined: goodbye

IIRC, try

$ go run hello.go goodbye.go

ie. name all the files of package main when using go run. If the files
are in $GOPATH, then you should be able to use also

$ go build # hello.go && goodby.go in current dir

or

$ go build foo/bar/hello # if the files are in $GOPATH/src/foo/bar/hello dir

Instead of 'build', 'install' is another option for non temporary
binaries. They get installed in $GOBIN (if not set then $GOROOT/bin, I
think).

HTH

-j

André Moraes

unread,
Sep 10, 2012, 7:47:11 AM9/10/12
to Sonia Hamilton, golan...@googlegroups.com
> % go run hello.go

"go run" isn't designed to make go act like a scripting language, it
will have to build your application every single time you run it.

Just use "go build" like Jan Mercl pointed, or use https://launchpad.net/gorun


--
André Moraes
http://amoraes.info

peterGo

unread,
Sep 10, 2012, 9:03:32 AM9/10/12
to golang-nuts
Sonia,

go run [build flags] gofiles... [arguments...]

Compile and run Go program
http://golang.org/cmd/go/#Compile_and_run_Go_program

You need to provide the gofiles... list or * for all go files in the
directory. For example,

$ go run hello.go goodbye.go
Hello
Goodbye

or

$ go run *
Hello
Goodbye

Peter

On Sep 10, 6:35 am, Sonia Hamilton <sonia.snowf...@gmail.com> wrote:
> I'm wondering how to split package main in to separate files. For example I
> have this large program which runs nicely:
>
> % cat hello.go
> package main
>
> import "fmt"
>
> func hello()   { fmt.Println("Hello"); return }
> func goodbye() { fmt.Println("Goodbye"); return }
> func main()    { hello(); goodbye() }
>
> % go run hello.go
> Hello
> Goodbye
>
> hello.go is getting too large, so I want to split it in to two files, *
> hello.go* and *goodbye.go*:

Sonia Hamilton

unread,
Sep 10, 2012, 6:31:48 PM9/10/12
to golan...@googlegroups.com
Thanks guys for all your replies - go run *.go works nicely.

Sonia.
Reply all
Reply to author
Forward
0 new messages