mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 04:48:29 +08:00
[Style] Change markdown source file to Unix-like style and add sample code
This commit is contained in:
@@ -23,16 +23,16 @@ func ReturnStr() string {
|
|||||||
在主程序 pack_test.go 中这个包通过声明的方式被导入
|
在主程序 pack_test.go 中这个包通过声明的方式被导入
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import “./pack1/pack1”
|
import "./pack1/pack1"
|
||||||
```
|
```
|
||||||
|
|
||||||
import 的一般格式如下:
|
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
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -46,8 +46,8 @@ func main() {
|
|||||||
var test1 string
|
var test1 string
|
||||||
test1 = pack1.ReturnStr()
|
test1 = pack1.ReturnStr()
|
||||||
fmt.Printf("ReturnStr from package1: %s\n", test1)
|
fmt.Printf("ReturnStr from package1: %s\n", test1)
|
||||||
fmt.Printf(“Integer from package1: %d\n”, pack1.Pack1Int)
|
fmt.Printf("Integer from package1: %d\n", pack1.Pack1Int)
|
||||||
// fmt.Printf(“Float from package1: %f\n”, pack1.pack1Float)
|
// fmt.Printf("Float from package1: %f\n", pack1.pack1Float)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -66,13 +66,13 @@ func main() {
|
|||||||
|
|
||||||
因此,按照惯例子目录和包之间有着密切的联系:为了区分不同包存放在不同的目录,每个包(所有属于这个包中的 go 文件)都存放在和包名相同的子目录下:
|
因此,按照惯例子目录和包之间有着密切的联系:为了区分不同包存放在不同的目录,每个包(所有属于这个包中的 go 文件)都存放在和包名相同的子目录下:
|
||||||
|
|
||||||
**Import with .** : import . “./pack1”
|
**Import with .** : import . "./pack1"
|
||||||
|
|
||||||
当使用.来做为包的别名时,你可以不通过包名来使用其中的项目。例如:`test := ReturnStr()`。
|
当使用.来做为包的别名时,你可以不通过包名来使用其中的项目。例如:`test := ReturnStr()`。
|
||||||
|
|
||||||
在当前的命名空间导入 pack1 包,一般是为了具有更好的测试效果。
|
在当前的命名空间导入 pack1 包,一般是为了具有更好的测试效果。
|
||||||
|
|
||||||
**Import with _** : import _ “./pack1/pack1”
|
**Import with _** : import _ "./pack1/pack1"
|
||||||
|
|
||||||
pack1包只导入其副作用,也就是说,只执行它的init函数并初始化其中的全局变量。
|
pack1包只导入其副作用,也就是说,只执行它的init函数并初始化其中的全局变量。
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ pack1包只导入其副作用,也就是说,只执行它的init函数并初
|
|||||||
|
|
||||||
通过以下方式,一次性安装,并导入到你的代码中:
|
通过以下方式,一次性安装,并导入到你的代码中:
|
||||||
|
|
||||||
import goex “codesite.ext/author/goExample/goex”
|
import goex "codesite.ext/author/goExample/goex"
|
||||||
|
|
||||||
因此该包的URL将用作导入路径。
|
因此该包的URL将用作导入路径。
|
||||||
|
|
||||||
|
@@ -26,12 +26,67 @@ sort 包
|
|||||||
```go
|
```go
|
||||||
func Float64sAreSorted
|
func Float64sAreSorted
|
||||||
|
|
||||||
type IntArray
|
type IntArray
|
||||||
|
|
||||||
func IntsAreSortedfunc IsSortedfunc Sort
|
func IntsAreSortedfunc IsSortedfunc Sort
|
||||||
|
|
||||||
func (IntArray) Len
|
func (IntArray) Len
|
||||||
|
|
||||||
func SortFloat64s
|
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 (IntArray) Less
|
|
||||||
|
使用通用的接口排序:
|
||||||
|
```
|
||||||
|
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)
|
||||||
|
23
eBook/examples/chapter_9/test.go
Normal file
23
eBook/examples/chapter_9/test.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
eBook/examples/chapter_9/uc.go
Normal file
7
eBook/examples/chapter_9/uc.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package uc
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
func UpperCase(str string) string {
|
||||||
|
return strings.ToUpper(str)
|
||||||
|
}
|
11
eBook/examples/chapter_9/ucmain.go
Normal file
11
eBook/examples/chapter_9/ucmain.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"./uc/uc"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str1 := "USING package uc"
|
||||||
|
fmt.Println(uc.UpperCase(str1))
|
||||||
|
}
|
Reference in New Issue
Block a user