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 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?