代码示例

package main

import "fmt"

func main(){
fmt.Println("Hello, world") // 定义数组的常规方法
/******************************
[32]byte // 长度为32的数组
[2 * N] struct {x, y int32} // 复杂类型数组
[100]*float32 // 指针数组
[2][3]int // 二维数组
[2][2][2] float64 // 等同于[2] ([2] ([2] float64))
******************************/
array := [5]int{1,2,3,4,5} // 遍历数组
fmt.Println("Traverse func 1 --->")
for i:= 0; i < len(array); i++ {
fmt.Printf("%d\t",array[i])
}
fmt.Println() for i, v := range array {
fmt.Printf("index %d is %d\n", i, v)
} // try to use modify func to modify array
// cannot success, value type
modifyArray(array)
fmt.Println("In main func(), array values -->", array) // use slice
useSlice()
} // 数组是值类型
func modifyArray(array [5]int){
array[0] = 12
fmt.Println("In Modify func(), array values -->", array)
} // 数组切片的结构定义
// 一个指向原生数组的指针
// 数据切片的元素个数
// 数组切片已分配的存储空间
func useSlice(){ // 1. 创建数组切片
// 基于数组
var myArr [3]int32 = [3]int32{23,1,3}
// myArray[first:last]
var mySlice []int32 = myArr[:] fmt.Println("Elements of myArr is")
for _, v := range myArr {
fmt.Printf("%d\t", v)
}
fmt.Println() fmt.Println("\nElement of mySlice is ")
for _, v := range mySlice {
fmt.Printf("%d\t", v)
}
fmt.Println() // 2. 直接创建
fmt.Println("直接创建数组切片")
// 创建一初始元素个数为5,初始值为0,预留10个元素的存储空间的slice
mySliceOne := make([]int, 5, 10)
fmt.Println("\nmySliceOne is ", mySliceOne) // 创建并且初始化包含5个元素的数组切片
mySliceTwo := []int{1,2,3,43,23}
fmt.Println("\nmySliceTwo is ", mySliceTwo) // 3. 遍历数组切片
for _, v := range mySliceTwo {
fmt.Printf("%d\t", v)
} // 4. 动态增减元素
// go语言内置cap和len两个函数
// cap返回的是数组切片的分配的空间大小,len返回的屙屎数组切片中当前所存储的元素个数
myNewSlice := make([]int, 5, 10)
fmt.Println("myNewSlice is ", myNewSlice)
fmt.Println("len(myNewSlice) is ", len(myNewSlice))
fmt.Println("cap(myNewSlice) is ",cap(myNewSlice)) // 5. 如果需要继续在myNewSlice中添加3个元素,使用append方法
myNewSlice = append(myNewSlice, 12, 12 , 23)
fmt.Println("after append 5 elements in slice, results is ", myNewSlice) // append后面的参数其实是不定参数, 也可以直接将一个slice添加到一个slice中,只是写法会有些不一样
subSlice := []int{12,2,1}
// 注意第二个参数后面的三个点
// 数组切片会自动处理内存空间不足的问题,如果内存不够,就会自动分配一块够大的空间
myNewSlice = append(myNewSlice, subSlice...)
fmt.Println("after append other slice in slice, results is ", myNewSlice) // 6. 基于数组切片创建数组切片
oldSlice := []int{12,23,12}
newSlice := oldSlice[1:2]
fmt.Println("newSlice is ", newSlice) // 7. 内容复制
// copy内置函数,如果两个数组切片不一样大,则按照其中个数少的那个进行复制
s1 := []int{1,2,34,4,5,6}
s2 := []int{4,21,1} // 只复制s1的前3个元素到s2中
fmt.Println("\ns2 is ", s2)
copy(s2, s1)
fmt.Println("copy elements of s1 to s2 is ", s2) // 只复制s2的3个元素到s2的前3个元素中
fmt.Println("\ns1 is ", s1)
copy(s1, s2)
fmt.Println("copy elements of s2 to s1 is ", s1) }

输出结果

Hello, world
Traverse func 1 --->
1 2 3 4 5
index 0 is 1
index 1 is 2
index 2 is 3
index 3 is 4
index 4 is 5
In Modify func(), array values --> [12 2 3 4 5]
In main func(), array values --> [1 2 3 4 5]
Elements of myArr is
23 1 3 Element of mySlice is
23 1 3
直接创建数组切片 mySliceOne is [0 0 0 0 0] mySliceTwo is [1 2 3 43 23]
1 2 3 43 23 myNewSlice is [0 0 0 0 0]
len(myNewSlice) is 5
cap(myNewSlice) is 10
after append 5 elements in slice, results is [0 0 0 0 0 12 12 23]
after append other slice in slice, results is [0 0 0 0 0 12 12 23 12 2 1]
newSlice is [23] s2 is [4 21 1]
copy elements of s1 to s2 is [1 2 34] s1 is [1 2 34 4 5 6]
copy elements of s2 to s1 is [1 2 34 4 5 6]

Go中的数组切片的使用总结的更多相关文章

  1. go语言中的数组切片:特立独行的可变数组

    go语言中的数组切片:特立独行的可变数组 初看go语言中的slice,觉得是可变数组的一种很不错的实现,直接在语言语法的层面支持,操作方面比起java中的ArrayList方便了许多.但是在使用了一段 ...

  2. GO中的数组切片

    GO中的数组切片可以看做是功能更强大的数组,在append数据时,可以自动调整内存大小以适应数据实际大小,有些类似于C#中的List<T>. GO 中数组切片的“容量”与实际储存的大小可以 ...

  3. Python中ndarray数组切片问题a[-n -x:-y]

    先看看如下代码: >>a=np.arange(10)>>a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>a[-7:] array( ...

  4. Go语言中的数组与数组切片

    Go中的数组与C的数组一样,只是定义方法不同 c: int a[10][10] Go [10][10]int 定义并初始化 array1 := [5]int{1,2,3,4,5} 变量名 := [in ...

  5. Go语言中底层数组和切片的关系以及数组扩容规则

    Go语言中底层数组和切片的关系以及数组扩容规则 demo package main import ( "fmt" ) func main() { // 声明一个底层数组,长度为10 ...

  6. golang中不定参数与数组切片的区别

    package main import "fmt" func main() { myfunc1(, , , ) //传递不定数量的参数 myfunc2([], , , }) //传 ...

  7. 窥探Swift之数组安全索引与数组切片

    今天是元宵节,祝大家元宵节快乐!在Swift中的数组和字典中下标是非常常见的,数组可以通过索引下标进行元素的查询,字典可以通过键下标来获取相应的值.在使用数组时,一个常见的致命错误就是数组越界.如果在 ...

  8. go语言 类型:数组切片

    初看起来,数组切片就像一个指向数组的指针,实际上它拥有自己的数据结构,而不仅仅是个指针.数组切片的数据结构可以抽象为以下3个变量: 1.一个指向原生数组的指针: 2.数组切片中的元素个数: 3.数组切 ...

  9. Shell中的数组及其相关操作

    http://blog.csdn.net/jerry_1126/article/details/52027539 Shell中数据类型不多,比如说字符串,数字类型,数组.数组是其中比较重要的一种,其重 ...

随机推荐

  1. 非PDC角色DC强制NTP

    前一阵,公司其他部门员工告诉我,他们的系统无法通过LDAP搜索账户了 经过检查,发现该服务器的时间居然比我们的时间服务器PDC快了将近20分钟,而且该问题机器的 时间源并非PDC,而是另外一台普通DC ...

  2. nmon 的下一代工具 njmon

    njmon njmon = nmon + JSON format + real-time push to a stats database + instant graphing of "al ...

  3. 用three.js开发三维地图实例

    公司要做智慧消防楼层可视化,需要用到web3d,开源的引擎中先研究了cesium三维地球,但cesium做楼层感觉是大材小用,而且体验也不好,最终选用的是功能强大.更适合小型场景的three. thr ...

  4. OSI 七层模型以及TCP/IP模型

    OSI 七层模型 定义 OSI(Open System Interconnection)即开放式系统互联通信参考模型.该模型是国际标准化组织(ISO)制定的一个用于计算机或通信系统间互联的标准体系,一 ...

  5. 最后の冲刺 for Alpha release 12/15/2015

    好开心啊,又吃成长快乐了~ 据说release前一天,团队的工作效率会达到顶峰.亲证无效!!! release当天才是团队智力效力能力的巅峰好不好?!因为,今天,plan B:福昕pdf reader ...

  6. Persona & User Scenario

    Persona: Tom:男,21岁,大学生,周末经常和同学们一起出去吃饭.唱歌.打球.郊游,期间会时不时拍一些照片以作纪念,长期积累的照片数量较多且内容繁杂,很少对照片进行整理: Alisa:女,2 ...

  7. Windows线程+进程通信

    一 Windows线程进程 1)定义 按照MS的定义, Windows中的进程简单地说就是一个内存中的可执行程序, 提供程序运行的各种资源. 进程拥有虚拟的地址空间, 可执行代码, 数据, 对象句柄集 ...

  8. spark 集群优化

    只有满怀自信的人,能在任何地方都怀有自信,沉浸在生活中,并认识自己的意志. 前言 最近公司有一个生产的小集群,专门用于运行spark作业.但是偶尔会因为nn或dn压力过大而导致作业checkpoint ...

  9. shiro:自定义remle(二)

    SpringMVC+SpringMVC+Mybatis项目 1:导入相关依赖 <dependencies> <!--测试依赖--> <dependency> < ...

  10. radio样式

    .radio{ position: relative; border: 1px solid #999; border-radius: 50%; width: 12px; height: 12px; b ...