diff --git a/eBook/09.5.md b/eBook/09.5.md index f60e0de..3fbe863 100755 --- a/eBook/09.5.md +++ b/eBook/09.5.md @@ -23,16 +23,16 @@ func ReturnStr() string { 在主程序 pack_test.go 中这个包通过声明的方式被导入 ```go -import “./pack1/pack1” +import "./pack1/pack1" ``` import 的一般格式如下: - import “包的路径或url地址“ 像 import "github.com/org1/pack1” + import “包的路径或url地址” 像 import "github.com/org1/pack1" 路径是指当前目录的相对路径。 -示例 9.5 [package_test.go](examples/chapter_9/package_test.go): +示例 9.5 [package_test.go](examples/chapter_9/book/package_test.go): ```go package main @@ -46,8 +46,8 @@ func main() { var test1 string test1 = pack1.ReturnStr() fmt.Printf("ReturnStr from package1: %s\n", test1) - fmt.Printf(“Integer from package1: %d\n”, pack1.Pack1Int) - // fmt.Printf(“Float from package1: %f\n”, pack1.pack1Float) + fmt.Printf("Integer from package1: %d\n", pack1.Pack1Int) + // fmt.Printf("Float from package1: %f\n", pack1.pack1Float) } ``` @@ -66,13 +66,13 @@ func main() { 因此,按照惯例子目录和包之间有着密切的联系:为了区分不同包存放在不同的目录,每个包(所有属于这个包中的 go 文件)都存放在和包名相同的子目录下: - **Import with .** : import . “./pack1” + **Import with .** : import . "./pack1" 当使用.来做为包的别名时,你可以不通过包名来使用其中的项目。例如:`test := ReturnStr()`。 在当前的命名空间导入 pack1 包,一般是为了具有更好的测试效果。 - **Import with _** : import _ “./pack1/pack1” + **Import with _** : import _ "./pack1/pack1" pack1包只导入其副作用,也就是说,只执行它的init函数并初始化其中的全局变量。 @@ -90,7 +90,7 @@ pack1包只导入其副作用,也就是说,只执行它的init函数并初 通过以下方式,一次性安装,并导入到你的代码中: - import goex “codesite.ext/author/goExample/goex” + import goex "codesite.ext/author/goExample/goex" 因此该包的URL将用作导入路径。 diff --git a/eBook/09.6.md b/eBook/09.6.md index d57494d..0cb3090 100644 --- a/eBook/09.6.md +++ b/eBook/09.6.md @@ -26,12 +26,67 @@ sort 包 ```go func Float64sAreSorted -type IntArray func IntsAreSortedfunc IsSortedfunc Sort - func (IntArray) Len func SortFloat64s - func (IntArray) Less func SortInts func (IntArray) Swap func SortStrings type Interface func StringsAreSorted type StringArray type Float64Array func (StringArray) Len func (Float64Array) Len - func (StringArray) Less func (Float64Array) Less - func (StringArray) Swap - func (Float64Array) Swap Other packages import “doc_example” ``` 使用通用的接口排序: - ``` func Float64sAreSorted[Top] func Float64sAreSorted(a []float64) bool func IntsAreSorted[Top] func IntsAreSorted(a []int) bool func IsSorted[Top] func IsSorted(data Interface) bool Test if data is sorted func Sort[Top] func Sort(data Interface) General sort function func SortInts[Top] func SortInts(a []int) Convenience wrappers for common cases type IntArray[Top] Convenience types for common cases: IntArray type IntArray []int +type IntArray + +func IntsAreSortedfunc IsSortedfunc Sort + +func (IntArray) Len + +func SortFloat64s + +func (IntArray) Less + +func SortInts + +func (IntArray) Swap + +func SortStrings type Interface + +func StringsAreSorted type StringArray type Float64Array + +func (StringArray) Len + +func (Float64Array) Len + +func (StringArray) Less + +func (Float64Array) Less + +func (StringArray) Swap + +func (Float64Array) Swap + +Other packages +import "doc_example" ``` - 如果你在一个团队中工作,并在源代码树被存储在网络硬盘上,就可以使用 godoc 给所有团队成员连续文档的支持。通过设置 sync_minutes=n,你甚至可以让它每 n 分钟自动更新您的文档! ## 链接 - [目录](directory.md) - 上一节:[自定义包和可见性](09.5.md) - 下一节:[使用 go install 安装自定义包](09.7.md) \ No newline at end of file + +使用通用的接口排序: +``` +func Float64sAreSorted[Top] +func Float64sAreSorted(a []float64) bool + +func IntsAreSorted[Top] +func IntsAreSorted(a []int) bool + +func IsSorted[Top] +func IsSorted(data Interface) bool +Test if data is sorted + +func Sort[Top] +func Sort(data Interface) +General sort function + +func SortInts[Top] +func SortInts(a []int) + +Convenience wrappers for common cases: type IntArray[Top] +Convenience types for common cases: IntArray type IntArray []int +``` + +如果你在一个团队中工作,并在源代码树被存储在网络硬盘上,就可以使用 godoc 给所有团队成员连续文档的支持。通过设置 sync_minutes=n,你甚至可以让它每 n 分钟自动更新您的文档! + +## 链接 + +- [目录](directory.md) +- 上一节:[自定义包和可见性](09.5.md) +- 下一节:[使用 go install 安装自定义包](09.7.md) diff --git a/eBook/examples/chapter_9/test.go b/eBook/examples/chapter_9/test.go new file mode 100644 index 0000000..340f036 --- /dev/null +++ b/eBook/examples/chapter_9/test.go @@ -0,0 +1,23 @@ +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) + } + } +} diff --git a/eBook/examples/chapter_9/uc.go b/eBook/examples/chapter_9/uc.go new file mode 100644 index 0000000..cf4a0ae --- /dev/null +++ b/eBook/examples/chapter_9/uc.go @@ -0,0 +1,7 @@ +package uc + +import "strings" + +func UpperCase(str string) string { + return strings.ToUpper(str) +} diff --git a/eBook/examples/chapter_9/ucmain.go b/eBook/examples/chapter_9/ucmain.go new file mode 100644 index 0000000..676cb47 --- /dev/null +++ b/eBook/examples/chapter_9/ucmain.go @@ -0,0 +1,11 @@ +package main + +import ( + "./uc/uc" + "fmt" +) + +func main() { + str1 := "USING package uc" + fmt.Println(uc.UpperCase(str1)) +}