Go-Slice

2024-09-04 21:58
文章标签 go slice

本文主要是介绍Go-Slice,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

要点

  • 一个区间:[low:high]
  • 两个概念:length, capability
  • 三个函数:make(), append(), copy()

概念

Array是在声明的时候都确定了长度,之后不可更改。Slice和数组类似,也是表示一个有序元素,但这个序列的长度可变。

在Go中,Slice序列中各元素的数据类型必须一致,因为在声明slice的时候即要指定类型。

可以在Python中找到Slice,和Go的Slice基本上是一致的语义和用法。Slice中文通常被译为切片,大抵是因为可以从一个序列中存取指定的某一部分。下面是Collins词典中对于slice的解释,有助于了解该词的含义:

  • N-COUNT (指食物切下的)片,薄片 A slice of bread, meat, fruit, or other food is a thin piece that has been cut from a larger piece. 语法信息

  • VERB 将…切成薄片 If you slice bread, meat, fruit, or other food, you cut it into thin pieces.

在第一次接触Python的slice的时候,对于这个slice或切片,觉得很是怪异,因为在C/C++、Java中还没有对应的术语。——虽然substr()的功用和slice有些相似,但显然slice的用法更加简洁和灵活,更显脚本语言的本色。

以上都是废话,下面给出示例。

声明&赋值

package main
import "fmt"/*
D:\examples>go run helloworld.go
values[0]=1
values[1]=2
values[2]=3
values[3]=4
values[4]=5D:\examples>
*/
func main() {var values []int // (1)values = make([]int, 5) // (2)for i, _ := range values {values[i] = i + 1}for i, item := range values {fmt.Printf("values[%d]=%d\n", i, item)}
}

要点:

  • (1)声明:和数组类似,但是不用指定长度。在这个时候,slice的长度为0,所以还不直接放数据。
  • (2)分配存储空间:调用内置make()函数为slice分配存储空间。
  • 接下来的存取则和普通的数组语法一致。

可以看到,slice和数组非常类似的,即内部存储是有序的数组形式,访问也是索引方式,仅仅在于其动态长度特性。如此,slice又和C++ STL的vector类似,不用指定长度。vector可以声明最大capability,可以动态改变长度。此为,slice和Java的ArrayList也是类似的。所以,通俗的讲,slice也可看做列表。——Go有一个list package,后面会提到。

用Array给slice赋值

package main
import "fmt"/*
D:\examples>go run helloworld.go
the_slice[0]=3
the_slice[1]=4
the_slice[2]=5D:\examples>
*/
func main() {the_array := [5]int {1, 2, 3, 4, 5} the_slice := the_array[2:5]for i, item := range the_slice {fmt.Printf("the_slice[%d]=%d\n", i, item)}
}

要点:
- 直接用[low:high]这种方式定义一个slice
- 这里的[low:high],用数学集合的方式来讲,就是[low, high),即左闭右开。
- 这种创建slice的方式,不需要make()函数。

[low:high]

Go的[low:high]和Python的语法一致。要点:
- [:]等价于[0:len(array)]
- [:n]等价于[0:n]
- [n:]等价于[n:len(array)]

注:别挑战low小于0或high大于len(array)的这种异常。

package main
import "fmt"/*
D:\examples>go run helloworld.go
[:] elements:
the_slice[0]=1
the_slice[1]=2
the_slice[2]=3
the_slice[3]=4
the_slice[4]=5
[2:] elements:
the_slice[0]=3
the_slice[1]=4
the_slice[2]=5
[:4] elements:
the_slice[0]=1
the_slice[1]=2
the_slice[2]=3
the_slice[3]=4
[:4] elements:
the_slice[0]=1
the_slice[1]=2
the_slice[2]=3
the_slice[3]=4
[1:4] elements:
the_slice[0]=2
the_slice[1]=3
the_slice[2]=4
panic: runtime error: slice bounds out of rangegoroutine 1 [running]:
panic(0x49cba0, 0xc04200a040)C:/Go/src/runtime/panic.go:500 +0x1af
main.main()D:/examples/helloworld.go:29 +0x191
exit status 2D:\examples>
*/
func main() {the_array := [5]int {1, 2, 3, 4, 5} the_slice := the_array[:]debug_slice(the_slice, "[:] elements:")debug_slice(the_array[2:], "[2:] elements:")debug_slice(the_array[:4], "[:4] elements:")debug_slice(the_array[:4], "[:4] elements:")debug_slice(the_array[1:4], "[1:4] elements:")// invalid slice index -1 (index must be non-negative)//debug_slice(the_array[-1:40], "[-1:40] elements:")//invalid slice index 40 (out of bounds for 5-element array)//debug_slice(the_array[0:40], "[:40] elements:")//Runtime Errorthe_slice2 := make([]int, 5)debug_slice(the_slice2[0:40], "[:40] elements:")
}func debug_slice(the_slice []int, msg string) {fmt.Println(msg)for i, item := range the_slice {fmt.Printf("the_slice[%d]=%d\n", i, item)}
}

length & capability

先给出示例代码:

package main
import "fmt"/*
D:\examples>go run helloworld.go
default values of slice:
the_slice[0]=0
the_slice[1]=0
the_slice[2]=0
the_slice[3]=0
the_slice[4]=0D:\examples>
*/
func main() {var the_slice []int;the_slice = make([]int, 5, 10)debug_slice(the_slice, "default values of slice:")
}func debug_slice(the_slice []int, msg string) {fmt.Println(msg)for i, item := range the_slice {fmt.Printf("the_slice[%d]=%d\n", i, item)}
}

在make()的时候,5表示长度为5个元素,此即length的概念;10表示整个容器的长度,即capability的概念。在这里,slice可以放10个元素,但目前只放了5个元素。make()的时候会自动初始化这5个元素为缺省值(???)。

在 <> Ch4 Composite Types, P88 有如下的描述:

make([]T, len)
make([]T, len, capability) // same as make([]T capability)[:len]

也就是说, 对于第一种情形,make直接创建长度为len的数组,并返回这个数组的元素。对于第二种情形,make会创建长度为capability的数组,但仅仅返回前面的len个元素。显然,返回的slice的长度就是len。

因为slice是依存于数组数据结构而存在,虽然第二种情形下slice的长度为len,但在添加新的元素的时候,只要不超过capability,就无须动态创建新的数组,而直接把要添加的元素放在slice已有元素的后面。

slice的length和capability的概念,和C++ STL vector对应的含义是一致的。——python的slice/list貌似没用这个概念。。。不确定。。。

当capability不至于放新的原始的时候,slice对象就会重新创建一个数组,并把原来的数据拷贝到新的数组里。

可以认为,capability对于功能无影响,但对于程序的性能会有影响。如果设置不当,会因为频繁的数据拷贝而降低性能。

TODO: 暂不清楚Go是否有OO的ctor的概念,如果有的话,用slice存放OO对象+ctor打印,会比较好的说明capability的作用。

对于以上解读,再给出一个例子:

package main
import "fmt"/*
D:\examples>go run helloworld.go
default value of slice:
the_slice[0]=0
the_slice[1]=0
the_slice[2]=0
slice:
the_slice[0]=1
the_slice[1]=2
the_slice[2]=3
the_slice[3]=4
the_slice[4]=5
the_slice[5]=6D:\examples>
*/
func main() {the_array := [6]int {1,2,3,4,5,6}the_slice := make([]int, 3, 5)debug_slice(the_slice, "default value of slice:")the_slice = the_array[:]debug_slice(the_slice, "slice:")
}func debug_slice(the_slice []int, msg string) {fmt.Println(msg)for i, item := range the_slice {fmt.Printf("the_slice[%d]=%d\n", i, item)}
}

append

append()函数是在slice后面添加元素。同前面对capability的描述,append()的时候可能涉及到数组的动态创建。

package main
import "fmt"/*
D:\examples>go run helloworld.go
slice1: 1       2       3
slice2: 1       2       3       4       5
slice1: 1       2       3       4       5       6
slice2: 1       2       3       4       5D:\examples>
*/
func main() {the_slice1 := []int {1,2,3}the_slice2 := append(the_slice1, 4, 5)debug_slice(the_slice1, "slice1:")debug_slice(the_slice2, "slice2:")the_slice1 = append(the_slice1, 4, 5, 6)debug_slice(the_slice1, "slice1:")debug_slice(the_slice2, "slice2:")//the_slice1.append undefined (type []int has no field or method append)//the_slice1.append(7)
}func debug_slice(the_slice []int, msg string) {fmt.Print(msg, "\t")for _, item := range the_slice {fmt.Print(item, "\t")}fmt.Println()
}

不清楚,为什么不支持.append()这种语法,而是append(slice, elements …)。

copy

copy()函数是把一个slice的“所有”元素拷贝到另外一个slice中。——是拷贝,而不是追加(append)。需要注意的是:如果目标slice的长度小于源slice的长度,那么就不会拷贝源slice的所有原始。

package main
import "fmt"/*
D:\examples>go run helloworld.go
slice1: 1       2       3
slice2: 0       0
slice3: 10      0       0       0
slice1: 1       2       3
slice2: 1       2
slice3: 1       2       3       0D:\examples>
*/
func main() {slice1 := []int {1,2,3}slice2 := make([]int, 2)slice3 := make([]int, 4)slice3[0] = 10debug_slice(slice1, "slice1:")debug_slice(slice2, "slice2:")debug_slice(slice3, "slice3:")copy(slice2, slice1)copy(slice3, slice1)debug_slice(slice1, "slice1:")debug_slice(slice2, "slice2:")debug_slice(slice3, "slice3:")
}func debug_slice(the_slice []int, msg string) {fmt.Print(msg, "\t")for _, item := range the_slice {fmt.Print(item, "\t")}fmt.Println()
}

附 append documentation

func append

func append(slice []Type, elems ...Type) []Type

The append built-in function appends elements to the end of a slice. If it has sufficient capacity, the destination is resliced to accommodate the new elements. If it does not, a new underlying array will be allocated. Append returns the updated slice. It is therefore necessary to store the result of append, often in the variable holding the slice itself:

slice = append(slice, elem1, elem2)
slice = append(slice, anotherSlice...)

As a special case, it is legal to append a string to a byte slice, like this:

slice = append([]byte("hello "), "world"...)

附 copy documentation

func copy

func copy(dst, src []Type) int

The copy built-in function copies elements from a source slice into a destination slice. (As a special case, it also will copy bytes from a string to a slice of bytes.) The source and destination may overlap. Copy returns the number of elements copied, which will be the minimum of len(src) and len(dst).

这篇关于Go-Slice的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137147

相关文章

Go路由注册方法详解

《Go路由注册方法详解》Go语言中,http.NewServeMux()和http.HandleFunc()是两种不同的路由注册方式,前者创建独立的ServeMux实例,适合模块化和分层路由,灵活性高... 目录Go路由注册方法1. 路由注册的方式2. 路由器的独立性3. 灵活性4. 启动服务器的方式5.

Go语言中三种容器类型的数据结构详解

《Go语言中三种容器类型的数据结构详解》在Go语言中,有三种主要的容器类型用于存储和操作集合数据:本文主要介绍三者的使用与区别,感兴趣的小伙伴可以跟随小编一起学习一下... 目录基本概念1. 数组(Array)2. 切片(Slice)3. 映射(Map)对比总结注意事项基本概念在 Go 语言中,有三种主要

Go Mongox轻松实现MongoDB的时间字段自动填充

《GoMongox轻松实现MongoDB的时间字段自动填充》这篇文章主要为大家详细介绍了Go语言如何使用mongox库,在插入和更新数据时自动填充时间字段,从而提升开发效率并减少重复代码,需要的可以... 目录前言时间字段填充规则Mongox 的安装使用 Mongox 进行插入操作使用 Mongox 进行更

Go语言利用泛型封装常见的Map操作

《Go语言利用泛型封装常见的Map操作》Go语言在1.18版本中引入了泛型,这是Go语言发展的一个重要里程碑,它极大地增强了语言的表达能力和灵活性,本文将通过泛型实现封装常见的Map操作,感... 目录什么是泛型泛型解决了什么问题Go泛型基于泛型的常见Map操作代码合集总结什么是泛型泛型是一种编程范式,允

基于Go语言实现一个压测工具

《基于Go语言实现一个压测工具》这篇文章主要为大家详细介绍了基于Go语言实现一个简单的压测工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录整体架构通用数据处理模块Http请求响应数据处理Curl参数解析处理客户端模块Http客户端处理Grpc客户端处理Websocket客户端

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Go语言实现将中文转化为拼音功能

《Go语言实现将中文转化为拼音功能》这篇文章主要为大家详细介绍了Go语言中如何实现将中文转化为拼音功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 有这么一个需求:新用户入职 创建一系列账号比较麻烦,打算通过接口传入姓名进行初始化。想把姓名转化成拼音。因为有些账号即需要中文也需要英

Go语言使用Buffer实现高性能处理字节和字符

《Go语言使用Buffer实现高性能处理字节和字符》在Go中,bytes.Buffer是一个非常高效的类型,用于处理字节数据的读写操作,本文将详细介绍一下如何使用Buffer实现高性能处理字节和... 目录1. bytes.Buffer 的基本用法1.1. 创建和初始化 Buffer1.2. 使用 Writ

Go Gorm 示例详解

《GoGorm示例详解》Gorm是一款高性能的GolangORM库,便于开发人员提高效率,本文介绍了Gorm的基本概念、数据库连接、基本操作(创建表、新增记录、查询记录、修改记录、删除记录)等,本... 目录1. 概念2. 数据库连接2.1 安装依赖2.2 连接数据库3. 数据库基本操作3.1 创建表(表关

Go信号处理如何优雅地关闭你的应用

《Go信号处理如何优雅地关闭你的应用》Go中的优雅关闭机制使得在应用程序接收到终止信号时,能够进行平滑的资源清理,通过使用context来管理goroutine的生命周期,结合signal... 目录1. 什么是信号处理?2. 如何优雅地关闭 Go 应用?3. 代码实现3.1 基本的信号捕获和优雅关闭3.2