2

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...

2

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.

  • I tried the two commends and both failed but the command which go returned the go path. The test with echo $GOPATH returned the project directory /Users/myUsername . – spk888 Jun 29 '18 at 12:26
  • What does echo $GOPATH and go version return? – Graham Miln Jun 29 '18 at 12:26
  • go version returns go version go1.10.3 darwin/amd64. About the Project Layout I think the structure is the same I just have another folder called pkg in the folder gocode. The command cd gocode export GOPATH=$PWD go run src/main.go does not run main.go for me. – spk888 Jun 29 '18 at 12:32
  • Glad this helped. See apple.stackexchange.com/help/someone-answers about how to accept an answer. Good luck with go, I find it a very productive language. – Graham Miln Jun 29 '18 at 12:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .