呵呵,看到有翻译好的,感觉自己翻译的好烂
所以不写了,想学习的自己去那看吧,比看我的强
[url]http://gotour.qizhanming.com[/url]
之前只找到了英文的,但是现在英文的打不开了,上面的中英文都有,很不错
[b]Slices[/b]
切片
A slice points to an array of values and also includes a length.
切片指向数组的值并且包括长度
[]T is a slice with elements of type T.
[]T是类型T带有元素的切片
package main
import "fmt"
func main() {
p := []int{2, 3, 5, 7, 11, 13}
fmt.Println("p ==", p)
for i := 0; i < len(p); i++ {
fmt.Printf("p[%d] == %d\n",
i, p[i])
}
}
输出:
p == [2 3 5 7 11 13]
p[0] == 2
p[1] == 3
p[2] == 5
p[3] == 7
p[4] == 11
p[5] == 13
Slices can be re-sliced, creating a new slice value that points to the same array.
切片可以再切,创建一个指向相同数组的新切片值
The expression
表达式
s[lo:hi]
evaluates to a slice of the elements from lo through hi-1, inclusive. Thus
切片元素取从lo到hi-1的值
s[lo:lo]
is empty and
空
s[lo:lo+1]
has one element.
有一个元素
package main
import "fmt"
func main() {
p := []int{2, 3, 5, 7, 11, 13}
fmt.Println("p ==", p)
fmt.Println("p[1:4] ==", p[1:4])
// missing low index implies 0
fmt.Println("p[:3] ==", p[:3])
// missing high index implies len(s)
fmt.Println("p[4:] ==", p[4:])
}
输出:
p == [2 3 5 7 11 13]
p[1:4] == [3 5 7]
p[:3] == [2 3 5]
p[4:] == [11 13]
Slices are created with the make function. It works by allocating a zeroed array and returning a slice that refers to that array:
切片用make函数创建,它工作通过分配一个0值数组并且返回一个关于那个数组的切片
a := make([]int, 5) // len(a)=5
Slices have length and capacity. A slice's capacity is the maximum length the slice can grow within the underlying array.
To specify a capacity, pass a third argument to make:
切片有长度和容量,一个切片的容量是它能在数组里面增长的最大长度
b := make([]int, 0, 5) // len(b)=0, cap(b)=5
Slices can be grown by "re-slicing" (up to their capacity):
切片能够通过重切增长(能够达到容量)
b = b[:cap(b)] // len(b)=5, cap(b)=5
b = b[1:] // len(b)=4, cap(b)=4
package main
import "fmt"
func main() {
a := make([]int, 5)
printSlice("a", a)
b := make([]int, 0, 5)
printSlice("b", b)
c := b[:2]
printSlice("c", c)
d := c[2:5]
printSlice("d", d)
}
func printSlice(s string, x []int) {
fmt.Printf("%s len=%d cap=%d %v\n",
s, len(x), cap(x), x)
}
输出:
a len=5 cap=5 [0 0 0 0 0]
b len=0 cap=5 []
c len=2 cap=5 [0 0]
d len=3 cap=3 [0 0 0]
The zero value of a slice is nil.
0值的切边是nil
A nil slice has a length and capacity of 0.
nil切片长度和容量都是0
package main
import "fmt"
func main() {
var z []int
fmt.Println(z, len(z), cap(z))
if z == nil {
fmt.Println("nil!")
}
}
输出:
[] 0 0
nil!
[b]Functions[/b]
Functions are values too.
函数也是值
package main
import (
"fmt"
"math"
)
func main() {
hypot := func(x, y float64) float64 {
return math.Sqrt(x*x + y*y)
}
fmt.Println(hypot(3, 4))
}
输出:
5
And functions are full closures.
函数是完全闭包
The adder function returns a closure. Each closure is bound to its own sum variable.
adder函数返回一个闭包,每个闭包绑定到它自己拥有的sum变量
package main
import "fmt"
func adder() func(int) int {
sum := 0
return func(x int) int {
sum += x
return sum
}
}
func main() {
pos, neg := adder(), adder()
for i := 0; i < 10; i++ {
fmt.Println(
pos(i),
neg(-2*i),
)
}
}
输出:
0 0
1 -2
3 -6
6 -12
10 -20
15 -30
21 -42
28 -56
36 -72
45 -90