dtziv24262 2012-06-03 09:33
浏览 174
已采纳

使用Git(或任何VCS)进行Go项目的正确组织是什么?

My GOPATH is /Users/joe/go. I am working on a project called myproj, which is a package called myproj.

If I want to be able to write import "myproj" then the directory structure I need is:

$GOPATH/src/myproj/myproj.go
$GOPATH/src/myproj/myproj_test.go
...

However, I can't seem to make this fit with Git. If I look at an example package from Google I see the following format:

go.example/hello/hello.go
go.example/LICENSE

Where go.example is the name of the repo.

So the actual package directories are found inside the repository. If I put the contents of this repository in a directory on my $GOPATH, e.g.

$GOPATH/src/go.example/hello/hello.go
$GOPATH/src/go.example/LICENSE

then I will have to type import "go.example/hello" rather than import "hello".

Coming back to my project, I need to package this up in a Git repository then I need a container directory. So my current file structure is:

$GOPATH/src/myproj                        # The dir for the git repo
$GOPATH/src/myproj/.git
$GOPATH/src/myproj/LICENSE                # Files in the base of the repo
$GOPATH/src/myproj/myproj/myproj.go       # Package files in package dir
$GOPATH/src/myproj/myproj/myproj_test.go  

I need the outer myproj directory to bound the git repository and I need the inner one to be the package directory. The upshot is that I need to type import "myproj/myproj" rather than import "myproj".

How do I fix this? Do I have to add multiple $GOPATHS, one for each project I'm developing?

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • dongli7870 2012-06-03 16:18
    关注

    First off:

    Do I have to add multiple $GOPATHs, one for each project I'm developing?

    No, you don’t need multiple $GOPATHs at all.

    They are a tool for managing your (potentially several) projects. When you set up a new project environment which you already know will have some dependencies - potentially unique to that project, or which should be similiar to other peoples setups, you create a new project folder and set it as the GOPATH. That way, you can also use (= check out) specific versions of a library for that project, while using other versions for your other projects in other project folders (= GOPATHs).

    As for your path issue: Go follows a generic paradigm of author/project (or organization/project). This will prevent naming clashes when several people, authors and organizations, start projects with the same names. (The author may then use sub-folders, sub-projects in “his” folder as well ofc.)

    If you are aware of this and still want to use only myproj as your package path, there is no problem in creating the git repository in that folder - in contrary to the example package you linked to.

    Coming back to my project, I need to package this up in a Git repository then I need a container directory.

    What makes you think so then? Go does not need it. Git does neither.

    So the following will work:

    /src/myproj/.git
    /src/myproj/myproj.go
    

    While it is not the encouraged practice, you can put the repository into your myproj folder.


    I tested this as follows:

    FOLDER
    FOLDER/src
    FOLDER/src/myproj
    FOLDER/src/myproj/myproj.go
    FOLDER/src/mainproj
    FOLDER/src/mainproj/main.go
    

    With folder/src/myproj/myproj.go

    package myproj
    
    type My struct {
        I int
    }
    

    and folder/src/mainproj/main.go

    package main
    
    import (
        "fmt"
        "myproj"
    )
    
    func main() {
        my := myproj.My{7}
        fmt.Printf("Works! %v", my.I)
    }
    

    Running

    cd FOLDER
    set GOPATH=FOLDER
    go run src/mainproj/main.go
    

    will output:

    Works! 7
    

    Now, if you git init in the folder FOLDER/src/myproj, that does not matter to Go itself at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格