`

Go语言旅行四 A Tour of Go

    博客分类:
  • Go
Go 
阅读更多
呵呵,看到有翻译好的,感觉自己翻译的好烂
所以不写了,想学习的自己去那看吧,比看我的强

http://gotour.qizhanming.com

之前只找到了英文的,但是现在英文的打不开了,上面的中英文都有,很不错

Slices
切片
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!

Functions

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

分享到:
评论

相关推荐

    A Tour of C++, 2nd Edition

    A Tour of C++ (2nd Edition) (C++ In-Depth Series) By 作者: Bjarne Stroustrup ISBN-10 书号: 0134997832 ISBN-13 书号: 9780134997834 Edition 版本: 2 出版日期: 2018-07-09 pages 页数: 815 In A Tour of ...

    C++语言导学 A Tour of C++(C++之父写的入门书)

    《计算机科学丛书:C++语言导学》作者是C++语言的设计者和最初实现者,写作本书的目的是让有经验的程序员快速了解C++现代语言。书中几乎介绍了C++语言的全部核心功能和重要的标准库组件,以很短的篇幅将C++语言的...

    手工安装GO官方教程A Tour of Go.docx

    A Tour of Go是官方提供的一个关于go语言短小精悍的教程,覆盖了包、变量、函数、数据类型等基础知识,也包括了方法、接口、并发编程等高级内容,读完并通过其中的练习题,短时间即可入门GO了。 该教程是一个WEB服务...

    A tour of C++ 2nd

    In A Tour of C++, Second Edition, Bjarne Stroustrup, the creator of C++, describes what constitutes modern C++. This concise, self-contained guide covers most major language features and the major ...

    A Tour of C++ 2nd

    A Tour of C++ 2nd ,c++学习资料

    A Tour of C++

    A Tour of C++ 一本不错的学习C++的书

    [mirror] A Tour of Go.zip

    [mirror] A Tour of Go

    A Tour of TensorFlow

    Abstract—Deep learning is a branch of artificial intelligence employing deep neural network architectures that has significantly advanced the state-of-the-art in computer vision, speech recognition, ...

    A Tour of C++/ C++语言导学

    高清非扫描版,带书签。Bjarne Stroustrup。。。。。。

    Bjarne Stroustrup A Tour of C++ (2013).pdf

    Bjarne Stroustrup A Tour of C++

    Whirlwind Tour Of Python

    如果你懂一些编程知识,比如学过C,Java等语言开发工具,或者你是一位具有工程背景的研究者或者工作者,那么这本书将会快速带你走进python世界,这是初学python者最佳学习资料,本附件是书中的练习题等源代码。...

    A Guided Tour Of Mathematical Physics (By Roel Snieder, Department Of Geophysics, Utrecht Univers.pdf

    A Guided Tour Of Mathematical Physics By Roel Snieder

    A wavelet tour of signal processing 3rd edition

    英文三版。 Mallet 的小波分析经典教程。全书13章800多页,非常详尽的介绍了小波分析原理和应用。很经典的教程。

    A Whirlwind Tour of Python

    对于想快速了解和使用python的人,这是一本非常好的入门书,与《python数据科学手册》为姊妹篇。

    C++语言导学.A Tour of C++.pdf

    《计算机科学丛书:C++语言导学》作者是C++语言的设计者和最初实现者,写作本书的目的是让有经验的程序员快速了解C++现代语言。书中几乎介绍了C++语言的全部核心功能和重要的标准库组件,以很短的篇幅将C++语言的...

    新版图书 A.Tour.of.C++.2nd.Edition.2018.7

    Stroustrup presents the C++ features in the context ... The tour covers some extensions being made for C++20, such as concepts and modules, and ends with a discussion of the design and evolution of C++.

    A Wavelet Tour of Signal Processing 3rd Edition

    Mallat的《信号处理的小波导引》的第三版 经典程度不用多说 PDF版本

Global site tag (gtag.js) - Google Analytics