drj58429 2014-12-19 19:22
浏览 109
已采纳

开发Golang套件,无法使用相对路径

I'm trying to develop a simple golang package

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

hello
   games
   game-utils

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

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

ok this worked well until I push to remote repo(e.g github.com) and try to use go get to install it. The problem was with the import path, I must change it to

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

the question is, everytime I develop the package I cannot import using "github.com/user/hello/game-utils" because obviously I wouldn't have pushed it to the remote repo, I need to import it using "./game-utils".

Is there any elegant way to fix this issue?

  • 写回答

1条回答 默认 最新

  • dongyan8896 2014-12-19 19:27
    关注

    Read this.

    You should always import it using:

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

    This is because of how the go tool works. It will look for it on the local machine in the directory: "GOPATH/src/github.com/user/hello/game-utils". As @JimB points out, the compiler always works with the local sources and the import paths are relative to GOPATH/src.

    The go get tool is the only one that looks for the sources on the internet. After getting them, it downloads them to "GOPATH/src/IMPORT_PATH" so the compiler and the other tools can now see them in their local structure.

    If you are creating a new project you should respect the same directory structure. If you are planning to upload your code to github then create manually the folder "GOPATH/src/github.com/YOUR-GITHUB-USER/PROYECT-NAME" and then initialize your git repo in there. (This works at least on git, hg, svn and github, bitbucket and google code)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?