macosgoosx-yosemite

Go: "stat hello.go: no such file or directory"


Just installed Go on Mac OS X, Yosemite Version 10.10.3, as explained in the Getting Started page of the official website:

Mac OS X package installer

Download the package file, open it, and follow the prompts to install the Go tools. The package installs the Go distribution to /usr/local/go.

The package should put the /usr/local/go/bin directory in your PATH environment variable. You may need to restart any open Terminal sessions for the change to take effect.

I am now in the Test your installation section, which states:

Check that Go is installed correctly by building a simple program, as follows.

Create a file named hello.go and put the following program in it:

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n") }

Then run it with the go tool:

$ go run hello.go
hello, world

If you see the "hello, world" message then your Go installation is working.

So, I created a hello.go file and, since I could not (ie: did not know how to) access the /usr/local/go/bin directory, I saved it on my desktop.

Obviously, I got the following error message:

stat hello.go: no such file or directory

So, where should I save my Go files to be able to run them?

UPDATE: after some research, I stumbled upond this video, explaining how to set the GOPATH.

If I want my Go workspace to be in user/code/go, how should I write my export GOPATH= command?


Solution

  • Following the explanation of Andrew Gerrand in Writing, building, installing, and testing Go code, I configured my Go workspace as follows:

    1. Create workspace

    First, since I want my Go code to be in my_user_name/code/go, I started by creating the corresponding folder:

    $ cd code
    $ mkdir go
    

    2. Setup GOPATH

    Then I configured the GOPATH:

    $ cd
    $ export GOPATH=$HOME/code/go
    $ cd code/go
    

    3. Add files to workspace

    Last, I manually (through the Finder) moved my hello.go file into the workspace and ran:

    $ go run hello.go
    

    Worked like a charm.