Golang教程:(十一)数组和切片
原文:https://golangbot.com/arrays-and-slices/ 这是本Golang系列教程的第十一篇。 数组数组是类型相同的元素的集合。例如,整数 5,8,9,79,76 的集合就构成了一个数组。Go不允许在数组中混合使用不同类型的元素(比如整数和字符串)。 声明数组的类型为 有很多声明数组的方式,让我们一个一个地介绍。 package main
import (
"fmt"
)
func main() {
var a [3]int //int array with length 3
fmt.Println(a)
}
数组的索引从 package main
import (
"fmt"
)
func main() {
var a [3]int //int array with length 3
a[0] = 12 // array index starts at 0
a[1] = 78
a[2] = 50
fmt.Println(a)
}
(译者注:可以用下标运算符( 可以利用速记声明(shorthand declaration)的方式来创建同样的数组: package main
import (
"fmt"
)
func main() {
a := [3]int{12, 78, 50} // shorthand declaration to create array
fmt.Println(a)
}
上面的程序输出为: (译者注:这个例子给出了速记声明的方式:在数组类型后面加一对大括号( 在速记声明中,没有必要为数组中的每一个元素指定初始值。 package main
import (
"fmt"
)
func main() {
a := [3]int{12}
fmt.Println(a)
}
上面程序的第 8 行: 在声明数组时你可以忽略数组的长度并用 package main
import (
"fmt"
)
func main() {
a := [...]int{12, 50} // ... makes the compiler determine the length
fmt.Println(a)
}
上面已经提到,数组的长度是数组类型的一部分。因此 package main
func main() {
a := [3]int{5, 8}
var b [5]int
b = a //not possible since [3]int and [5]int are distinct types
}
在上面程序的第 6 行,我们试图将一个 数组是值类型在 Go 中数组是值类型而不是引用类型。这意味着当数组变量被赋值时,将会获得原数组(译者注:也就是等号右面的数组)的拷贝。新数组中元素的改变不会影响原数组中元素的值。 package main
import "fmt"
func main() {
a := [...]string{"USA","China","India","Germany","France"}
b := a // a copy of a is assigned to b
b[0] = "Singapore"
fmt.Println("a is ",a)
fmt.Println("b is ",b)
}
上面程序的第 7 行,将数组 a is [USA China India Germany France] b is [Singapore China India Germany France] 同样的,如果将数组作为参数传递给函数,仍然是值传递,在函数中对(作为参数传入的)数组的修改不会造成原数组的改变。 package main
import "fmt"
func changeLocal(num [5]int) {
num[0] = 55
fmt.Println("inside function ",num)
}
func main() {
num := [...]int{5, 6, 7, 8, 8}
fmt.Println("before passing to function ",num)
changeLocal(num) //num is passed by value
fmt.Println("after passing to function ",num)
}
上面程序的第 13 行,数组 before passing to function [5 6 7 8 8]
inside function [55 6 7 8 8]
after passing to function [5 6 7 8 8]
数组的长度内置函数 package main
import "fmt"
func main() {
a := [...]float64{67.7, 89.8, 21, 78}
fmt.Println("length of a is",len(a))
}
上面程序的输出为: 使用 range 遍历数组
package main
import "fmt"
func main() {
a := [...]float64{67.7, 78}
for i := 0; i < len(a); i++ { //looping from 0 to the length of the array
fmt.Printf("%d th element of a is %.2fn",i,a[i])
}
}
上面的程序使用 0 th element of a is 67.70
1 th element of a is 89.80
2 th element of a is 21.00
3 th element of a is 78.00
Go 提供了一个更简单,更简洁的遍历数组的方法:使用 range for。range 返回数组的索引和索引对应的值。让我们用 range for 重写上面的程序(除此之外我们还计算了数组元素的总和)。 package main
import "fmt"
func main() {
a := [...]float64{67.7, 78}
sum := float64(0)
for i,v := range a {//range returns both the index and value
fmt.Printf("%d the element of a is %.2fn",v)
sum += v
}
fmt.Println("nsum of all elements of a",sum)
}
上面的程序中,第 8 行 0 the element of a is 67.70
1 the element of a is 89.80
2 the element of a is 21.00
3 the element of a is 78.00
sum of all elements of a 256.5
如果你只想访问数组元素而不需要访问数组索引,则可以通过空标识符来代替索引变量: for _,v := range a { //ignores index
}
上面的代码忽略了索引。同样的,也可以忽略值。 多维数组目前为止我们创建的数组都是一维的。也可以创建多维数组。 package main
import (
"fmt"
)
func printarray(a [3][2]string) {
for _,v1 := range a {
for _,v2 := range v1 {
fmt.Printf("%s ",v2)
}
fmt.Printf("n")
}
}
func main() {
a := [3][2]string{
{"lion","tiger"},{"cat","dog"},{"pigeon","peacock"},//this comma is necessary. The compiler will complain if you omit this comma
}
printarray(a)
var b [3][2]string
b[0][0] = "apple"
b[0][1] = "samsung"
b[1][0] = "microsoft"
b[1][1] = "google"
b[2][0] = "AT&T"
b[2][1] = "T-Mobile"
fmt.Printf("n")
printarray(b)
}
上面的程序中,第 17 行利用速记声明创建了一个二维数组 在第 23 行声明了另一个二维数组 第 7 行声明的函数 lion tiger
cat dog
pigeon peacock
apple samsung
microsoft google
AT&T T-Mobile
以上就是对数组的介绍。尽管数组看起来足够灵活,但是数组的长度是固定的,没办法动态增加数组的长度。而切片却没有这个限制,实际上在 Go 中,切片比数组更为常见。 切片切片(slice)是建立在数组之上的更方便,更灵活,更强大的数据结构。切片并不存储任何元素而只是对现有数组的引用。 创建切片元素类型为 package main
import (
"fmt"
)
func main() {
a := [5]int{76, 77, 79, 80}
var b []int = a[1:4] //creates a slice from a[1] to a[3]
fmt.Println(b)
}
通过 下面是创建切片的另一种方式: package main
import (
"fmt"
)
func main() {
c := []int{6, 8} //creates and array and returns a slice reference
fmt.Println(c)
}
在上面的程序中,第 9 行 修改切片切片本身不包含任何数据。它仅仅是底层数组的一个上层表示。对切片进行的任何修改都将反映在底层数组中。 package main
import (
"fmt"
)
func main() {
darr := [...]int{57, 89, 90, 82, 100, 67, 69, 59}
dslice := darr[2:5]
fmt.Println("array before",darr)
for i := range dslice {
dslice[i]++
}
fmt.Println("array after",darr)
}
上面程序的第 9 行,我们创建了一个从 array before [57 89 90 82 100 78 67 69 59]
array after [57 89 91 83 101 78 67 69 59]
当若干个切片共享同一个底层数组时,对每一个切片的修改都会反映在底层数组中。 package main
import (
"fmt"
)
func main() {
numa := [3]int{78, 79 ,80}
nums1 := numa[:] //creates a slice which contains all elements of the array
nums2 := numa[:]
fmt.Println("array before change 1",numa)
nums1[0] = 100
fmt.Println("array after modification to slice nums1",numa)
nums2[1] = 101
fmt.Println("array after modification to slice nums2",numa)
}
可以看到,在第 9 行, array before change 1 [78 79 80]
array after modification to slice nums1 [100 79 80]
array after modification to slice nums2 [100 101 80]
从输出结果可以看出,当多个切片共享同一个数组时,对每一个切片的修改都将会反映到这个数组中。 切片的长度和容量切片的长度是指切片中元素的个数。切片的容量是指从切片的起始元素开始到其底层数组中的最后一个元素的个数。 (译者注:使用内置函数 让我们写一些代码来更好地理解这一点。 package main
import (
"fmt"
)
func main() {
fruitarray := [...]string{"apple","orange","grape","mango","water melon","pine apple","chikoo"}
fruitslice := fruitarray[1:3]
fmt.Printf("length of slice %d capacity %d",len(fruitslice),cap(fruitslice)) //length of is 2 and capacity is 6
}
在上面的程序中,创建了一个以
切片的长度可以动态的改变(最大为其容量)。任何超出最大容量的操作都会发生运行时错误。 package main
import (
"fmt"
)
func main() {
fruitarray := [...]string{"apple","chikoo"}
fruitslice := fruitarray[1:3]
fmt.Printf("length of slice %d capacity %dn",cap(fruitslice)) //length of is 2 and capacity is 6
fruitslice = fruitslice[:cap(fruitslice)] //re-slicing furitslice till its capacity
fmt.Println("After re-slicing length is","and capacity is",cap(fruitslice))
}
在上面的程序中, 第 length of slice 2 capacity 6
After re-slicing length is 6 and capacity is 6
用 make 创建切片内置函数 package main
import (
"fmt"
)
func main() {
i := make([]int, 5, 5)
fmt.Println(i)
}
用 追加元素到切片我们已经知道数组是固定长度的,它们的长度不能动态增加。而切片是动态的,可以使用内置函数 x …T 表示 你可能会问一个问题:如果切片是建立在数组之上的,而数组本身不能改变长度,那么切片是如何动态改变长度的呢?实际发生的情况是,当新元素通过调用 package main
import (
"fmt"
)
func main() {
cars := []string{"Ferrari","Honda","Ford"}
fmt.Println("cars:",cars,"has old length",len(cars),"and capacity",cap(cars)) //capacity of cars is 3
cars = append(cars,"Toyota")
fmt.Println("cars:","has new length",cap(cars)) //capacity of cars is doubled to 6
}
在上面的程序中, cars: [Ferrari Honda Ford] has old length 3 and capacity 3
cars: [Ferrari Honda Ford Toyota] has new length 4 and capacity 6
切片的 0 值为 package main
import (
"fmt"
)
func main() {
var names []string //zero value of a slice is nil
if names == nil {
fmt.Println("slice is nil going to append")
names = append(names,"John","Sebastian","Vinay")
fmt.Println("names contents:",names)
}
}
在上面的程序中 slice is nil going to append
names contents: [John Sebastian Vinay]
可以使用 package main
import (
"fmt"
)
func main() {
veggies := []string{"potatoes","tomatoes","brinjal"}
fruits := []string{"oranges","apples"}
food := append(veggies,fruits...)
fmt.Println("food:",food)
}
上面的程序中,在第10行将 切片作为函数参数可以认为切片在内部表示为如下的结构体: type slice struct {
Length int
Capacity int
ZerothElement *byte
}
可以看到切片包含长度、容量、以及一个指向首元素的指针。当将一个切片作为参数传递给一个函数时,虽然是值传递,但是指针始终指向同一个数组。因此将切片作为参数传给函数时,函数对该切片的修改在函数外部也可以看到。让我们写一个程序来验证这一点。 package main
import (
"fmt"
)
func subtactOne(numbers []int) {
for i := range numbers {
numbers[i] -= 2
}
}
func main() {
nos := []int{8, 6}
fmt.Println("slice before function call",nos)
subtactOne(nos) //function modifies the slice
fmt.Println("slice after function call",nos) //modifications are visible outside
}
在上面的程序中,第 17 行将切片中的每个元素的值减 array before function call [8 7 6]
array after function call [6 5 4]
多维切片同数组一样,切片也可以有多个维度。 package main
import (
"fmt"
)
func main() {
pls := [][]string {
{"C","C++"},{"JavaScript"},{"Go","Rust"},}
for _,v1 := range pls {
for _,v2)
}
fmt.Printf("n")
}
}
上面程序的输出如下: C C++
JavaScript
Go Rust
内存优化切片保留对底层数组的引用。只要切片存在于内存中,数组就不能被垃圾回收。这在内存管理方便可能是值得关注的。假设我们有一个非常大的数组,而我们只需要处理它的一小部分,为此我们创建这个数组的一个切片,并处理这个切片。这里要注意的事情是,数组仍然存在于内存中,因为切片正在引用它。 解决该问题的一个方法是使用 copy 函数 package main
import (
"fmt"
)
func countries() []string {
countries := []string{"USA","Singapore","Australia"}
neededCountries := countries[:len(countries)-2]
countriesCpy := make([]string,len(neededCountries))
copy(countriesCpy,neededCountries) //copies neededCountries to countriesCpy
return countriesCpy
}
func main() {
countriesNeeded := countries()
fmt.Println(countriesNeeded)
}
在上面程序中,第 9 行 我(原文作者)已经将我们讨论的所有概念汇总到一个程序中,你可以从 github 下载。 数组和切片的介绍到此结束。感谢阅读。 目录 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |