doulangdang9986 2018-07-19 01:53
浏览 37
已采纳

如何组织GO项目包(如果它将托管在不同的仓库中)(GitHub和SourceForge)

Say I want to create a project and host it on GitHub,I HAVE TO create project struct like this:

src/github.com/user/      
     myproject/
     main.go
     util/
       fileutil.go

and in the main.go ,I have to write the import as:

import github.com/user/myproject/util/fileutil

AND now I also want to host this project onto SourceForge, should I copy the whole project and modify the path ?? it seems not so good enough. Is there any other way to do that ? What I need is just create my project under src folder, and can be hosted to any repo as I wish, without changing the packages.

  • 写回答

1条回答 默认 最新

  • dongzhang3482 2018-07-19 03:25
    关注

    If developing an app and not a library

    If you don't plan to have other projects "import" your own project (this is, if you are implementing an application and not a library), it might be possible for you to avoid any reference to github.com in your local paths.

    Note: this is not the recommended approach, but from my tests it does work, you can compile, run and test your code if you structure it this way.

    You can create your project structure as follows:

    src/
        myproject/
            main.go
            util/
                fileutil.go
    

    Then you can import like this:

    import "myproject/util/fileutil"
    

    You can then host the contents of the myproject folder anywhere without changing paths within the project files.

    If developing a library

    If you are actually developing a library others will be able to import, then it gets more complicated since those projects will actually need a full path to import your project.

    You can create a "vanity" import path, like myproject.io/... by using the meta tag like described here:

    https://golang.org/cmd/go/#hdr-Remote_import_paths

    That way, when the go tool queries your myproject.io/ pages, you should respond with a header like this:

    <meta name="go-import" content="myproject.io git https://github.com/user/myproject">
    

    And then change that header you return if you decide to move away from github.

    Note that this does not prevent users to import your project directly from github, if you want to avoid that you need to use the technique described in the canonical import path spec:

    https://golang.org/doc/go1.4#canonicalimports

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

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码