Files
the-way-to-go_ZH_CN/eBook/09.8.md
2014-09-19 16:51:13 +08:00

124 lines
4.5 KiB
Markdown
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#9.8 自定义包:目录结构,go install和go test
为了示范我们创建了一个名为uc的简单包,它含有一个UpperCase函数将字符串的所有字母转换为大写。当然这并不值得创建一个自己包同样的功能已被包含在"strings"包里,但是同样的技术也可以应用在更复杂的包中。
##9.8.1 自定义包的目录结构
下面的结构给了你一个好的示范(uc代表通用包名, 名字为粗体的代表目录,斜体代表可执行文件):
/home/user/goprograms
ucmain.go (uc包主程序)
Makefile (ucmain的2-makefile)
ucmain
src/uc (包含uc包的go源码)
uc.go
uc_test.go
Makefile (包的1-makefile)
uc.a
_obj
uc.a
_test
uc.a
bin (包含最终的执行文件)
ucmain
pkg/linux_amd64
uc.a (包的目标文件)
将你的项目放在goprograms目录下(你可以创建一个环境变量GOPATH,参考2.2/3章节:在.profile和.bashrc文件中添加export GOPATH=/home/user/goprograms)而你的项目将作为src的子目录。uc包 中的功能在uc.go中实现。
示例 9.6 [uc.go](exmaples/chapter_9/uc.go)
package uc
import "strings"
func UpperCase(str string) string {
return strings.ToUpper(str)
}
包通常附带一个或多个测试文件在这我们创建了一个uc_test.go文件如9.8章节所述
示例 9.7 [test.go](examples/chapter_9/uc.go)
package uc
import "testing"
type ucTest struct {
in, out string
}
var ucTests = []ucTest {
ucTest{"abc", "ABC"},
ucTest{"cvo-az", "CVO-AZ"},
ucTest{"Antwerp", "ANTWERP"},
}
func TestUC(t *testing.T) {
for _, ut := range ucTests {
uc := UpperCase(ut.in)
if uc != ut.out {
t.Errorf("UpperCase(%s) = %s, must be %s", ut.in, uc,
ut.out)
}
}
}
通过指令编译并安装包到本地go install src/uc, 这会将uc.a复制到pkg/linux_amd64下面
另外使用make,通过以下内容创建一个包的Makefile(1)在src/uc目录下:
include $GOROOT/src/Make.inc
TARG=uc
GOFILES=\
uc.go\
include $(GOROOT)/scr/Make.pkg
在该目录下的命令行调用: gomake
这将创建一个_obj目录并将包编译生成的存档uc.a放在该目录下
这个包可以通过go test测试
创建一个ud.a的测试文件在目录下输出为PASS时测试通过
在13.8章节我们将给出另外一个测试例子并进行深入研究
备注有可能你当前的用户不具有足够的资格使用go install(没有权限)。这种情况下选择root用户su。确保Go环境变量和Go源码路径也设置给su同样也适用你的普通用户(详见2.3章节)
接下来我们创建主程序ucmain.go:
示例 9.8 [ucmain.go](/examples/chapter_9/ucmain.go)
package main
import (
"fmt"
"./uc/uc"
)
func main() {
str1 := "USING package uc"
fmt.Println(uc.UpperCase(str1))
}
然后在这个目录下输入go install
另外复制uc.a到uc目录并创建一个Makefile(2)并写入文本:
包含在$GOROOT/src/Make.inc
TARG=ucmain
GOFILES=\
ucmain.go\
include $GOROOT/src/Make.cmd
执行gomake编译ucmain.go到ucmain目录
运行./ucmain显示: USING package uc!
## 9.8.2 本地安装包
本地包在用户目录下:
使用给出的目录结构,以下命令用来从源码安装本地包:
go install /home/user/goprograms/src/uc # 编译安装uc
cd /home/user/goprograms/uc
go install ./uc # 编译安装uc和之前的指令一样
cd ..
go install . # 编译安装ucmain
安装到$GOROOT下
如果我们想安装的包在系统上的其他Go程序中被使用它一定要安装到$GOROOT下。
这样做,在.profile和.bashrc中设置GOPATH=$GOROOT。然后执行go install uc将会
(1) 复制源代码到$GOROOT/src/pkg/linux_amd64/uc
(2) 复制包存档到$GOROOT/PKG/LINUX_AMD64/uc
uc包可以通过"import uc"在任何Go程序中被引用。
## 9.8.3 依赖系统的代码
不同操作系统上运行不同的程序是非常少见的:绝大多数情况下语言和标准库解决了大部分的可移植性问题。
你有一个很好的理由去写平台平台特定的代码,例如汇编语言。这种情况下,按照下面的约定是合理的:
prog1.go
prog1_linux.go
prog1_darwin.go
prog1_windows.go
prog1.go定义了不同操作系统通用的接口并将系统特定的代码写到prog1_os.go中。
对于Go工具你可以指定prog1_$GOOS.go or prog1_$GOARCH.go
或在平台Makefile中 prog1_$(GOOS).go\ or prog1_$(GOARCH).go\
示例 9.6: package strev
运用前面9.7章节所有技术使用strev包解决练习9.2。