Go语言旅行四 A Tour of Go

呵呵,看到有翻译好的,感觉自己翻译的好烂
所以不写了,想学习的自己去那看吧,比看我的强

[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
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值