duaedf6509
duaedf6509
2013-09-30 17:26

Go项目的目录结构正确吗?

已采纳

I'm relatively new to Go and I've recently created a project that's going up on GitHub. I've tried to follow guides but there's a pressing question of why my binaries end up in src/?

My layout is like this:

ssm/ - Name of project
    LICENSE
    README.md
    src/ - Source Code
        files.go - All my source code is here.
        src - The compiled binary ends up here
    bin/ - Binaries

I set my $GOPATH to ~/Documents/Programming/Go/. From my gopath, I can't type go build ssm because it cannot find package. If I cd into the directory, it complains it can't load package: package .: no Go source files.

I have to actually go into src and compile there, which means the binary isn't in bin/.

What am I doing wrong?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • duanhuiqing9528 duanhuiqing9528 8年前

    See https://code.google.com/p/go-wiki/wiki/GithubCodeLayout

    To be compatible with go get, your project's package name needs to be fully-qualified under the github.com domain:

    $GOPATH/
        src/github.com/<user>/ssm/
            .git
            LICENSE
            README.md
            files.go
        bin/
    

    Note that the base of the git repository (.git) is not the same as the $GOPATH.

    Also, go build <package> will output a compiled executable to the current directory. If you want the exe to go to bin/, use go install <package> instead.

    点赞 评论 复制链接分享
  • dtlygweb2017 dtlygweb2017 4年前

    Your go code you can kept in a workspace. A workspace contains many source files (git, hg, svm, etc.). The Go tool understand the layout. We don't require Makefile or build.xml here. The basic directory layout is everything. If in any case if you are going to change the file layout, accordingly you need to change the build.

    This is the general structure you can follow,

    $GOPATH/

    src/
        github.com/username/repo/
            mypkg/
                mysrc1.go
                mysrc2.go
            cmd/mycmd/
                main.go
    bin/
        mycmd
    

    And, this is the standard workspace

    $GOPATH/

    bin/fixhub                              # installed binary
    pkg/darwin_amd64/                       # compiled archives
        code.google.com/p/goauth2/oauth.a
        github.com/...
    src/                                    # source repositories
        code.google.com/p/goauth2/
            .hg
            oauth                           # used by package go-github
            ...
        github.com/
            golang/lint/...                 # used by package fixhub
                .git
            google/go-github/...            # used by package fixhub
                .git
            dsymonds/fixhub/
                .git
                client.go
                cmd/fixhub/fixhub.go        # package main
    

    go get fetch many repositories whereas go install builds a binary out of them. It's convenient and easy to go for go development quickly. And, everyone in the go community follows the same. This puts src, bin and pkg into the home directory. And, $HOME/bin is already in our path before creating our workspace.

    点赞 评论 复制链接分享

相关推荐