csdnceshi62 2012-04-03 00:20 采纳率: 0%
浏览 314
已采纳

组织多文件的 Go 项目

Note: this question is related to this one, but two years is a very long time in Go history.

What is the standard way to organize a Go project during development ?

My project is a single package mypack, so I guess I put all the .go files in a mypack directory.

But then, I would like to test it during development so I need at least a file declaring the main package, so that I can do go run trypack.go

How should I organize this ? Do I need to do go install mypack each time I want to try it ?

转载于:https://stackoverflow.com/questions/9985559/organizing-a-multiple-file-go-project

  • 写回答

7条回答 默认 最新

  • bug^君 2012-04-03 00:26
    关注

    I would recommend reviewing this page on How to Write Go Code

    It documents both how to structure your project in a go build friendly way, and also how to write tests. Tests do not need to be a cmd using the main package. They can simply be TestX named functions as part of each package, and then go test will discover them.

    The structure suggested in that link in your question is a bit outdated, now with the release of Go 1. You no longer would need to place a pkg directory under src. The only 3 spec-related directories are the 3 in the root of your GOPATH: bin, pkg, src . Underneath src, you can simply place your project mypack, and underneath that is all of your .go files including the mypack_test.go

    go build will then build into the root level pkg and bin.

    So your GOPATH might look like this:

    ~/projects/
        bin/
        pkg/
        src/
          mypack/
            foo.go
            bar.go
            mypack_test.go
    

    export GOPATH=$HOME/projects

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

报告相同问题?