如何将Golang项目(一组包)放到Github上?

huangapple go评论100阅读模式
英文:

How to place Golang project (a set of packages) to Github?

问题

这是一个来自Golang教程http://golang.org/doc/code.html的示例项目工作空间(目录结构):

bin/
    hello              # 命令可执行文件
pkg/
    linux_amd64/       # 这将反映您的操作系统和架构
        github.com/user/
            newmath.a  # 包对象
src/
    github.com/user/
        hello/
            hello.go   # 命令源代码
        newmath/
            sqrt.go    # 包源代码

那么,我需要做什么,我需要在这个工作空间中的哪里进行git init,以便以后能够:

  1. 仅将newmath包导入到我的某个独立项目中。这样做:

     import "github.com/user/newmath"
    
  2. 仅获取hello.exe可执行文件。

  3. 获取整个项目工作空间(所有目录:bin,pkg,src)。

英文:

It is not clear for me from Golang tutorial how to put Golang code to Github to be able to import that code as a package from Github later.

This is an example project-workspace (directory structure) from the Golang tutorial http://golang.org/doc/code.html:

bin/
    hello              # command executable
pkg/
    linux_amd64/       # this will reflect your OS and architecture
        github.com/user/
            newmath.a  # package object
src/
    github.com/user/
        hello/
            hello.go   # command source
        newmath/
            sqrt.go    # package source

So, what do I need to do, where do I need to git init in this workspace, to be able later:

  1. To import only newmath package into my some separate project. This way:

     import "github.com/user/newmath"
    
  2. To get only hello.exe executable.

  3. To get the whole project-workspace (all directories: bin, pkg, src).

答案1

得分: 37

  1. 对于包newmath来说,与后面的步骤相同

     $ mkdir $GOPATH/src/github.com/username/newmath
     $ cd $GOPATH/src/github.com/username/newmath
     $ git init
     $ ...更多的git设置
     $ touch sqrt.go
     $ gvim sqrt.go
     $ git add sqrt.go
     $ git commit -a -m '初始提交'
     $ git push
    

    现在人们可以执行

     $ go get github.com/username/newmath
    

    并且

    import "github.com/username/newmath"

    现在应该在他们的源代码中工作。该包将自动按需安装。

  2. 我假设hello命令和newmath包没有关联,或者关联不紧密,不属于一个单独的存储库。

     $ mkdir $GOPATH/src/github.com/username/hello
     $ cd $GOPATH/src/github.com/username/hello
     $ git init
     $ ...更多的git设置
     $ touch hello.go
     $ gvim hello.go
     $ git add hello.go
     $ git commit -a -m '初始提交'
     $ git push
    

    现在人们可以执行

     $ go get github.com/username/hello
     $ go install github.com/username/hello
    

    来安装你的hello命令。

    • 在托管服务中发布$GOPATH/pkg的内容几乎没有意义。
    • 在托管服务中发布$GOPATH/bin的内容有一定意义。但是出于明显的原因,我不鼓励这种做法。此外,如果您发布源代码,则二进制文件是不必要的,每个人都可以构建自己的(可信任的)二进制文件。

您似乎对术语“工作区”还有些困惑。一个工作区通常只在开发者的机器上存在一次,但它通常包含多个存储库。有些是开发者编写的,其他的则是从互联网上“go get”来的。在这种情况下,发布整个工作区几乎没有意义。

然而,有些人每个项目、每个存储库甚至每个包使用一个单独的工作区。我不知道这样做的好处是什么。或者更准确地说,我认为与单个工作区相比,没有任何好处,单个工作区可以通过export GOPATH=$HOME来定义(这是我多年来一直使用的方式,没有任何问题)。

英文:
  1. For the package newmath it's the same as (later 2.)

    $ mkdir $GOPATH/src/github.com/username/newmath
    $ cd $GOPATH/src/github.com/username/newmath
    $ git init
    $ ... more git setup
    $ touch sqrt.go
    $ gvim sqrt.go
    $ git add sqrt.go
    $ git commit -a -m 'Inital commit'
    $ git push
    

    Now people can do

    $ go get github.com/username/newmath
    

    and

    import "github.com/username/newmath"

    should now work in their sources. The package will be installed on
    demand automatically.

  2. I'll assume that the hello command and the newmath package are
    not related, or not enough tightly related to belong to a single
    repository.

    $ mkdir $GOPATH/src/github.com/username/hello
    $ cd $GOPATH/src/github.com/username/hello
    $ git init
    $ ... more git setup
    $ touch hello.go
    $ gvim hello.go
    $ git add hello.go
    $ git commit -a -m 'Inital commit'
    $ git push
    

    Now people can do

    $ go get github.com/username/hello
    $ go install github.com/username/hello
    

    to install your command hello.

    • It makes almost no sense to publish the content of $GOPATH/pkg at
      the hosting service.
    • It makes some sense to publish the content of $GOPATH/bin at the hosting service. But I discourage this practice for obvious
      reasons. Additionally, if you're publishing the sources - the
      binaries are not necessary and everybody can build their (trusted)
      own.

You seem to be perhaps still a bit confused by the term 'workspace'. A workspace is quite often existing only once at the developer's machine, yet it the typically contains several repositories. Some authored by the developer, others "go getted" from the Internet. To publish a whole wokspace in this case makes little sense.

However, there are people using a separate workspace per project or per repository or maybe even per package. I don't know what the benefits are. Or better said, I think that there are none compared to the single workspace, defined by, say export GOPATH=$HOME (as is my case for years w/o any trouble with it for years).

答案2

得分: 2

请查看以下链接以获取更多详细信息:

github go wiki on github code layout

下面是一个永久的部分:

应用程序和两个库都存放在Github上,每个都有自己的存储库。
$GOPATH是项目的根目录 - 每个Github存储库都会被检出到$GOPATH的几个子文件夹中。

你的代码布局应该如下所示:

$GOPATH/
src/
    github.com/
        jmcvetta/
            useless/
                .git/
                useless.go
                useless_test.go
                README.md
            uselessd/
                .git/
                uselessd.go
                uselessd_test.go
                README.md

src/github.com/jmcvetta/下的每个文件夹都是一个单独的git检出根目录。

英文:

Check this link out for more details:

github go wiki on github code layout

below is a permanent section:

> The app and both libraries live on Github, each in its own repository.
> $GOPATH is the root of the project - each of your Github repos will be
> checked out several folders below $GOPATH.
>
> Your code layout would look like this:

$GOPATH/
src/
    github.com/
        jmcvetta/
            useless/
                .git/
                useless.go
                useless_test.go
                README.md
            uselessd/
                .git/
                uselessd.go
                uselessd_test.go
                README.md

> Each folder under src/github.com/jmcvetta/ is the root of a separate git checkout.

答案3

得分: 0

如果你不喜欢使用git命令行界面,那么你只需要通过网页界面将代码上传到GitHub仓库即可。只需确保你的包名与仓库名相同(小写),然后就可以开始了。我在github.com/Digitalblueeye/enroute上也是这样做的,用于我的REST API库。

英文:

If your not a fan of git cli then all you really need to do is upload to github repo via the web interface. Just make sure that you have your package name the same name as the repo (lowercase) and you should be good to go. I did just the same with github.com/Digitalblueeye/enroute for my REST API library.

huangapple
  • 本文由 发表于 2013年6月3日 23:58:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/16900938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定