我从Golang教程中不清楚如何将Golang代码放到Github上,以便能够在稍后从Github导入该代码作为包.
这是来自Golang教程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
Run Code Online (Sandbox Code Playgroud)
那么,我需要做什么,git init
在这个工作区中需要做什么,以便以后能够:
仅将newmath
包导入我的某个单独项目.这条路:
import "github.com/user/newmath"
Run Code Online (Sandbox Code Playgroud)只获得hello.exe
可执行文件.
获取整个项目工作区(所有目录:bin,pkg,src).
zzz*_*zzz 35
对于包装,newmath
它与(后来的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
Run Code Online (Sandbox Code Playgroud)
现在人们可以做到
$ go get github.com/username/newmath
Run Code Online (Sandbox Code Playgroud)
和
import "github.com/username/newmath"
现在应该在他们的来源工作.该软件包将自动按需安装.
我假设hello
命令和newmath
包不相关,或者与属于单个存储库紧密相关.
$ 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
Run Code Online (Sandbox Code Playgroud)
现在人们可以做到
$ go get github.com/username/hello
$ go install github.com/username/hello
Run Code Online (Sandbox Code Playgroud)
安装你的命令hello
.
$GOPATH/pkg
托管服务的内容几乎没有意义.$GOPATH/bin
的托管服务.但出于显而易见的原因,我劝阻这种做法.此外,如果您要发布源代码 - 二进制文件不是必需的,并且每个人都可以构建自己的(受信任的).您似乎对"工作空间"这个术语感到有些困惑.工作空间通常只在开发人员的计算机上存在一次,但它通常包含多个存储库.一些由开发人员撰写,其他人则从互联网上"哄骗".在这种情况下发布整个wokspace毫无意义.
但是,有些人在每个项目或每个存储库中使用单独的工作空间,甚至可能每个包使 我不知道有什么好处.或者更好地说,我认为没有什么比单个工作空间(由...定义的那样)export GOPATH=$HOME
(多年来我的情况多年没有任何问题).