It does not work because your foobar.go
source file is not in a directory called foobar
. go build
and go install
try to match directories, not source files.
- Set
$GOPATH
to a valid directory, e.g.export GOPATH="$HOME/go"
- Move
foobar.go
to$GOPATH/src/foobar/foobar.go
and building should work just fine.
Additional recommended steps:
- Add
$GOPATH/bin
to your$PATH
by:PATH="$GOPATH/bin:$PATH"
- Move
main.go
to a subfolder of$GOPATH/src
, e.g.$GOPATH/src/test
go install test
should now create an executable in$GOPATH/bin
that can be called by typingtest
into your terminal.