Golang package development cannot use relative path

I am trying to develop a simple golang package

let's say its name is "Hello", directory structure looks like below

hello
   games
   game-utils

      

then in hello.go (main code) I have:

import (
    gameUtils "./game-utils"
    "./games"
)

      

ok it worked until I pushed to the remote repo (like github.com) and try to use go get

to install it. The problem was the import path, I have to change it to

import (
    gameUtils "github.com/user/hello/game-utils"
    "github.com/user/hello/games"
)

      

The question is, every time I develop a package, I can't import with "github.com/user/hello/game-utils"

, because obviously I wouldn't push it to the remote repo, I need to import it with "./game-utils"

.

Is there an elegant way to solve this problem?

+3


source to share


1 answer


Read this .

You should always import it using:

import "github.com/user/hello/game-utils"

      



This is because of the way the go tool works. He will look for it on the local machine in our catalog "GOPATH/src/github.com/user/hello/game-utils"

. As @JimB points out, the compiler always works with local sources and the import paths refer to GOPATH/src

.

The tool go get

is the only one looking for sources on the Internet. After receiving them, it loads them into "GOPATH/src/IMPORT_PATH"

so that the compiler and other tools can now see them in their local structure.

If you are creating a new project, you must follow the same directory structure. If you plan on uploading your code to github then manually create a folder "GOPATH/src/github.com/YOUR-GITHUB-USER/PROYECT-NAME"

and then initialize the git repository there. (This works at least git

, hg

, svn

and github

, bitbucket

, and google code

)

+4


source







All Articles