Can't run go file with go run (filename) I just started getting into Golang and wanted to run a file in the terminal with go run and then the file name which is main.go

I configured the GOPATH like so:

export GOROOT="/usr/local/go"   
export GOPATH="/Users/myUsername/Desktop/gocode/"  
export PATH="/Users/myUsername/Desktop/gocode/bin:$PATH"


I created a folder on my desktop named gocode, placed three folders inside named src, pkg and bin.

And code is stored in src and when I want to run the code I get the following message.
my command was go run main.go
the output I received was  stat main.go: no such file or directory  

I have no clue why it won't find the directory...
To run the .go file and not the compiled binary, you need to give go run the relative source file path. Try:

go run src/main.go


To compile the go program into a binary executable, use:

go build src/main.go


This will work if...


go can be found; test this with the command which go. Does it return a path to go?
Your GOPATH environment is set appropriately; test this with echo $GOPATH. Does it return the project directory? See go wiki page on GOPATH for advice.


Break Down the Problem

Open a new Terminal.app window. This will discard previous export commands that may be causing confusion.

Test go can be found. Use which go to ensure the binary is available. If not, fix this with an export command.

Check the directory structure of your project. Make sure no names are mispelt and your .go files have the correct suffix. The Finder may be hiding the true suffix. Does ls -lR list the contents you expect?

Check you are in the right directory. The go commands need to be executed from within the project directory. Use pwd to see what directory you are in? Is it the project directory gocode/? If not, use a cd command to fix this.

Project Layout

Let's assume your file structure follows the minimal go convention:

gocode/
gocode/bin/
gocode/src/
gocode/src/main.go


The three separate commands to run main.go would be:

cd gocode
export GOPATH=$PWD
go run src/main.go


This will run the main function within main.go. To build an executable suitable for distribution, use the command go build src/main.go.

Managing GOPATH Directories

With regard to managing gopaths and projects, I have had success with Herbert Fischer's Manage multiple GOPATH dirs with ease approach.