Skip to content

Instantly share code, notes, and snippets.

@MarkBennett
Created September 22, 2012 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkBennett/3767324 to your computer and use it in GitHub Desktop.
Save MarkBennett/3767324 to your computer and use it in GitHub Desktop.
Failing to import Go packages

I've just installed Go from the package on the golang site.

When trying to build (or run) import getting an import error, but can't figure out why:

go build world2.go

This produces the error:

# world2.go:3:8: import "world": cannot find package

Running "go env" produces:

GOROOT="/usr/local/go" GOBIN="/usr/local/go/bin" GOARCH="amd64" GOCHAR="6" GOOS="darwin" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64" GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common" CGO_ENABLED="1"

This is what my file structure looks like (ala tree):

. ├── world.go └── world2.go

Any tips what I'm doing wrong or next steps?

package world
import "fmt"
func Greeting() {
fmt.Println("Ola!")
}
package main
import "world"
func main() {
world.Greeting()
}
@darkhelmet
Copy link

world.go has to be in directory $GOPATH/src/world and then naturally it has to be compiled.

@MarkBennett
Copy link
Author

@darkhelmet So if I'm working in $HOME/go-hello does that mean I should add that to my $GOPATH? Or do you normally add the current working directory to your GOPATH?

Right now, my $GOPATH = $HOME/.gopath (set in my .bash_profile)

@dearing
Copy link

dearing commented Sep 23, 2012

Sit back and read up at http://golang.org/doc/code.html#GOPATH it is difficult to grasp at first and isn't like some other languages where you create some packages for use in the same project all at once. Anything you package up is considered to be a publicly fetch-able package, even if you don't.

So you make a 'world' package and set that in your GOPATH - something like ~/src/mystuff for stuff that isn't really public, plop all your 'world' package code in there.

Then you change your imports statement to something like:

import "mystuff/world"

Now the go tools know to search your GOPATH for this package code in the ~/src/mystuff/world folder. But it may no be compiled from that src folder yet so run:

go get mystuff/world

to have your current project to compile up the world package and be ready for linking when you are ready to

go build

Now if you wanted others to really use your world package you could publish it on somewhere public like github as import github.com/me/mystuff/world and then anyone who wanted to build your code could just go get github.com/me/mystuff/world or I think even go get all now.

One last note: in the project folder the go build command will look for package main and compile all the files that it is told to, no need to specify go build world2.go.

g'luck

@dearing
Copy link

dearing commented Sep 23, 2012

Now if all you want to do is export something from a single source then just capitalize it as you had done in Greeting. If it is apart of the same package then it is visible.

package world

import "fmt"

func Greeting() {
    fmt.Println("Ola!")
}
package main

func main() {
    Greeting()
}

go build

Ola!

@dearing
Copy link

dearing commented Sep 23, 2012

Oops, I meant like follows:

world.go

package main

import "fmt"

func Greeting() {
    fmt.Println("Ola!")
}

world2.go:

package main

func main() {
    Greeting()
}

@Gayu1
Copy link

Gayu1 commented Nov 23, 2015

Hi I got stuck with an error.
is it possible to call build.go file from other file?

@Gayu1
Copy link

Gayu1 commented Nov 23, 2015

I wanna call build.go file from another file

@Gayu1
Copy link

Gayu1 commented Nov 23, 2015

can anyone help plzz?

@aa-software2112
Copy link

@dearing thank you so much for this, I have been endlessly searching for how to import a local package! So much fluff on all the other explanations. Bang on!

@dearing
Copy link

dearing commented Nov 9, 2019

@aa-software2112 things have evolved since this discussion but I am happy my 7 year old comments have a net positive. Please read up on modules and vendoring and message me if you think I can help in any way.

@SA1MAANKHAN
Copy link

hey , I am using atom ...but still cannot import local packages....can anyone help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment