I'm at the point where want to organize my Go web server into packages. Currently I have everything in a few files and I simply type: 'go run server.go foo.go bar.go'
How do I organize my files so I don't need to keep adding files to the command line. I've investigated the GOPATH variable but it doesn't seem to work.
export GOPATH=$HOME/myserver
I moved my files to the src/ subdirectory.
myserver/src/server.go
myserver/src/foo.go
myserver/src/bar.go
Shouldn't 'go run' search $HOME/myserver/src for all go files?
I've tried these examples but they don't work.
go run server.go; # Doesn't work
go run src/server.go; # Doesn't work
By the way, all files are in 'package main'
This info is covered really well on golang.org
Read this about how to write Go code
And this about organizing go code
Tip: you can run go run *
to run all files in a folder
Your above example should look something like this
$GOPATH=$HOME
The GOPATH should have:
$GOPATH
src/
pkg/
bin/
$GOPATH/src
is where you would store your source code for each go project
$GOPATH/src/myserver
would contain your myserver program
cd to $GOPATH/src/myserver
and run go install
and you would now have your myserver
binary located at $GOPATH/bin/myserver
Add the location of your bin to your path export PATH=$PATH:$GOPATH/bin
and you can run myserver
to start your go program
go run
will look for a file (or wildcard) in your current working directory.
If you would like to run your programs from anywhere, either use go build
same as you would go run
and move the binaries where appropriate or, better yet, set your $GOBIN
environment variable and add it your $PATH
-- then run go install *
in your project directory.
It's also probably a good idea to make specific directories for projects instead just dumping it all in $GOPATH/src