Go 工作空间 深度解析
本文章来自于 谷歌官方的翻译: How to Write Go Code? 介绍这篇文档举例证明了一个简单地 Go package 并且介绍了 如果要使用 go tool,那么就必须将代码组织成一种特殊的形式。 请仔细的阅读这篇文档,它会教你采用最简单的方法来安装运行你的Go程序。 代码组织Workspace 工作空间Go tool 被设计成用来开源公共仓库中的代码,尽管或许你不需要发布你的代码, 但是环境的设置还是一样的。 Go的源代码必须存放在 workspace中。 workspace 是一个目录(directory hierachy),在这个目录下有三个子目录
go tool 编译(build) 源代码文件(source packages),并且将 resulting binaries 安装在 src 目录下面包含多个 版本控制的仓库, (比如 Git 或者 Mercurial) 用来 跟踪一个或者多个 source package的开发进度。 下面给你一个真实环境中的 workspace 是什么样子的: bin/
hello # 可执行的命令
outyet # 可执行的命令
pkg/
linux_amd64/
github/golang/example/
stringutil.a # pakcage objects
src/
github.com/golang/example/
.git/ # git repository metadata
hello/
hello.go # command source
outyet/
main.go # command source
main_test.go # test source
stringuitl/
reverse.go # package source
reverse_test.go # test source
这个工作空间 包含一个 代码仓库repository( example仓库),example 仓库由两个命令 command (hello,outyet)和一个库library组成(stringutil) 一个典型的 workspace 会包含多个 source repositories,包含很多 pakcages 和 commands。 大部分的 Go programmers 会 keep all their Go source code and dependencies in a single workspace. (将所有的Go源代码和依赖保存在一个工作空间中)。 另外 从上述例子中也可以看到, bin/ 中命令的名字,pkg/中库的名字 都是文件夹的文字。 src/中包的名字和你 import 时候的名字不一定一样,这一点要区别。 GOPATH 环境变量
开始编程: GOPATH目录可以是任意的,唯一的要求是不能是你安装Go时的目录(/usr/local/go)。 $ mkdir $HOME/go
$ export GOPATH=$HOME/go
为了方便,将工作空间的 bin 子目录 加入到 你的 $ export PATH=$PATH:$GOPATH/bin
Package pathes 包路径packages path 不是 library path,虽然二者有联系。 如果你将你的代码保存在一个 源仓库中(source repository),那么你应该使用 the root of that source repository as your base path. 注意: 你或许不需要发布你的代码 to remote repository before you can build it. 但是将代码组成成 code repository是一个好的习惯。 举例:我们使用 $ mkdir -p $GOPATH/src/github.com/usr
第一个 program为了编译和运行一个简单地程序,首先选择一个 package path(这里使用 github.com/usr),创建一个 package directory (包目录)
第二:在包目录下创建一个文件 名为 package main
import "fmt"
func main() {
fmt.Println("Hello,World.n")
}
第三: 使用 go tool 来 build and install 这个程序 $ go install github.com/user/hello
注意: 你可以你的计算机的任何文件夹下面运行这个命令。 当然, 如果你在 package directory 下面运行 $ cd $GOPATH/github.com/usr/hello
$ go install
这个命令会 build the 当有错误发生的时候, 现在你可以 通过输入 $ $GOPATH/bin/hello
hello,world
或者如果你已经将 $ hello
hello, world
如果你使用源代码版本控制系统,现在这是一个好的时机来 initialize a repository, add the files,and commit you first chagne. Again,this step is optional: you do not need to use source control to write Go code. $ cd $GOPATH/src/github.com/usr/hello
$ git init
Initialized empty Git repository in /home/user/go/src/github.com/user/hello/.git/
$ git add hello.go
$ git commit -m "initial commit"
[master (root-commit) 0b4507d] initial commit
1 file chagned,1 insertion(+)
create mode 100644 hello.go
将代码 Push 到远程的仓库留作读者作为一个联系吧。 第一个 Library现在我们写一个 Library,并且在 hello 程序中使用这个库。
$ mkdir $GOPATH/src/github.com/usr/stringutil
// Package stringutil contains utility functions for working with strings.
//Reverse returns its argument string reversed run-wise left to right.
func Reverse(s string) string {
r := []rune(s)
for i,j := 0,len(r)-1; i < len(r)/2; i,j = i+1,j-1 {
r[i],r[j] = r[j],r[i]
}
return string(r)
}
$ go build github.com/user/stringutil
或者你在 这个目录下面,只需要 $ go build
即可。
$ go install
package main
import (
"fmt"
"github.com/usr/stringutil"
)
func main() {
fmt.Printf(stirngutil.Reverse("!oG,olleH"))
}
$ go install github.com/user/hell
运行新版本的程序,你会看见如下结果: $ hello
Hello,Go!
经过了上述的步骤,你的工作空间现在应该是下面的情况: bin/
hello # command executalbe 可执行命令
pkg/
linux_amd64/ # this will reflect you OS and architecture
github.com/usre/
stringutil.a # package object
src/
github.com/user/
hello/
hello.go # command source
stringutil/
reverse.go # package source
可以看到, Go command exectualbes are statically linked。(go command 执行是静态链接的) The package objects need not be present to run Go program.(pakcage object 在Go程序执行的时候是不需要的)。 Package names在Go源文件中, 第一行语句一定是 package name
其中, Go的惯例是, package name 是 import path 的最后一个元素: 比如,导入的包是 可执行的命令 必须 使用 包名 没有必要在 所有的包中(linked to a single binary)是唯一的,之哟啊 import path(它们的 full file name)是唯一的就可以了。 TestingGo 有一个轻量级的测试框架: Tesing。 Testing 由 Testing 可以用作单元测试,也可以用来进行压力测试。 如何使用 Testing 写单元测试: Testing 测试框架会 运行每个测试函数,一旦这个测试函数 调用了 failure function, 比如: 举例: package stringutil
import "testing"
func TestReverse(t *testing.T) {
cases := []struct {
in,want string
} {
{"Hello,World","dlrow,olleH"}
{"Hello,Go","oG,olleH"}
{"",""}
}
for _,c := range case {
got := Reverse(c,in)
if got != c.want {
t.Errorf("Reverse(%q) == %q,want %q",c.in,got,c.want)
}
}
}
$ go test github.com/user/stringutil
ok github.com/user/stringutil 0.165s
As always,如果你在 package directory下面运行 $ go test
ok github.com/user/stringutil 0.165s
如果你有疑问可以,运行go help test 并且看一下 testing package documentation 了解详情。 Remote packages (远程包安装)
举例, 在 Github $ go get github.com/golang/example/hello
$ $GOPATH/bin/hello
Hello,Go examples!
如果这个指定的 package 没有在 workspace中,那么 在 issuing bin/
hello # 可执行的命令
pkg/
linux_amd64/
github/golang/example/
stringutil.a # pakcage objects
github/user/
stringutil.a # package objects
src/
github.com/golang/example/
.git/ # git repository metadata
hello/
hello.go # command source
outyet/
main.go # command source
main_test.go # test source
stringuitl/
reverse.go # package source
reverse_test.go # test source
github.com/user/
hello/
hello.go # command source
stringutil/
reverse.go # package source
reverse_test.go # test source
Github中的 hello 命令 依赖于同一个代码仓库中的 stringutil。hello.go 文件中的 imports 需要使用同样的 import path convention(惯例),这样
这个惯例很重要,大家一定要遵守。并且这个惯例 可以让你的包很轻松地被别人使用。 Go Wiki和godoc.org提供了一些列的 external Go progects. What’s next ? (接下来干什么)订阅 golang-announce邮件列表,这样当有一个新的 go 版本出来的时候,会通知你。 阅读 Effective Go 来写出干净 优美的 Go 代码。 阅读 A Tour of Go 来学习 Go的语法 访问 Documentation Page 上面有一些文章, 来深度了解 Go 语言 和它的工具,以及它的库。 看一下 Go Documentation 上面也有很多不错的资源。 看一下 Go Command 了解一下 Go 的 命令的详细用法。 Go Nuts Go 语言的官方讨论列表。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |