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
$GOPATHto a valid directory, e.g.export GOPATH="$HOME/go" - Move
foobar.goto$GOPATH/src/foobar/foobar.goand building should work just fine.
Additional recommended steps:
- Add
$GOPATH/binto your$PATHby:PATH="$GOPATH/bin:$PATH" - Move
main.goto a subfolder of$GOPATH/src, e.g.$GOPATH/src/test go install testshould now create an executable in$GOPATH/binthat can be called by typingtestinto your terminal.