4

I have an 'example.go' file where I'm trying to import a directory in the same folder.

I have 'example.go' and the 'lucky' dir in the same folder called 'project'.

Here's how I'm trying to import the 'lucky' dir:

import (
    "fmt"
    golucky "goLucky"
    "io/ioutil"
    "os"
)

But when I run example.go, it looks like it is trying to import it from the go source because it throws the error:

    cannot find package "goLucky" in any of:
    /usr/local/go/src/pkg/goLucky (from $GOROOT)
    ($GOPATH not set)

How can I import the local folder in the same dir as the file?

6
  • @chengbo what if I want to import an online dir. like github.com/philipsoutham/golucky/v0.0.1, how can I do that? Commented Mar 27, 2014 at 20:53
  • 1
    set your GOPATH, and then go get github.com/xxxxx, import github.com/xxx/yyy see golang.org/doc/code.html#remote Commented Mar 27, 2014 at 21:05
  • @chengbo okay, thank you. How do I set my GOPATH? Commented Mar 27, 2014 at 21:10
  • export GOPATH=/path/to/go, see golang.org/doc/code.html#GOPATH Commented Mar 27, 2014 at 21:13
  • @chengbo Thank you. I made the directory look how it is supposed to. But when I Run $ GoPath = /Projects/dirName/, I get -bash: GOPATH: command not found Commented Mar 27, 2014 at 21:35

1 Answer 1

1

You need to set your GOPATH environment variable and locate your lucky dir within that. See http://golang.org/doc/code.html#Organization

So for example, if you set GOPATH=~, then put your lucky.go file in ~/src/lucky/lucky.go then you should be able to import "lucky" successfully.

Sign up to request clarification or add additional context in comments.

4 Comments

what do I type in my .go file to set the GoPath?
GOPATH is an environment variable and is not set in your .go file, it is set in your shell or system settings.
Oh, okay. I made the directory look how it is supposed to. But when I Run $ GoPath = /Projects/dirName/, I get -bash: GOPATH: command not found
Please read the documentation I've already linked. As indicated both in the wikipedia article and the Go documentation, you must run export GOPATH=... in order to set its value in bash (other shells may be different).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.