当前位置:网站首页>[golang learning notes 2.0] arrays and slices in golang
[golang learning notes 2.0] arrays and slices in golang
2022-07-27 07:38:00 【Who made the promise】
Introduction of the array
- Arrays can hold multiple Same type of data , Arrays are also a data type , stay go The array in is a value type .
- Definition of array : var Array name [ Array size ] data type
go Precautions for the use of arrays in
- An array is a combination of multiple data of the same type , Once the array is defined , Its length is fixed , Can't change dynamically .
- The elements in the array can be of any data type , Including value types and reference types , But don't mix .
- After the array is created If there is no assignment . Arrays have default values .int Default 0 string Default empty string "",bool type Default false
- The subscript of the array is 0 Start with 【 Basically, arrays in most languages are subscripts 0 Start with 】
- go The array is a value type , By default, the value will be copied blindly . Arrays will not affect
- If you want to use it in other functions , To modify the original array . You can use reference passing .
Introduction to slicing
- Slice array get a reference , Therefore, the type of slice is reference type , So when it's worth passing , Follow the reference passing mechanism .
- Slicing is similar to array . The length of the slice can be changed . So slice can be understood as defining an empty array 【 Be similar to php in $arr=array()】
- Basic syntax for slice definition var Variable name [] type ; example :var arr [] int
- The slice is equivalent to a structure from the bottom ;
type slice struct{
ptr *[2]int
len int
cap int
}
Several ways to declare slices
- The first one is : Declare an array of unspecified size to define the slice
package main
import (
"fmt"
)
func main() {
// Declare an array of unspecified size to define the slice
var slice []int
// It is impossible to assign values directly like this , An error will be reported that the index is out of range
// slice[0] =4
// This situation requires append Function to append data
slice = append(slice, 1)
slice = append(slice, 2)
slice = append(slice, 3)
// You can re assign values within the existing index range in this way ,slice[3]=5 This is not going to work .
slice[2] =4
// Print the results slice: [1 2 4]
fmt.Printf("slice: %v\n", slice)
}
- The second kind : Use make Function to declare a slice
package main
import (
"fmt"
)
func main() {
// Declare an array of unspecified size to define the slice
//make( Type of slice , The length of the slice , The volume of the slice )
var slice []int=make([]int, 10,20)
// Print the results :slice: [0 0 0 0 0 0 0 0 0 0]
fmt.Printf("slice: %v\n", slice)
}
- The third kind of : Use make Function to declare a slice
package main
import (
"fmt"
)
func main() {
// Assign values directly ; Be similar to make
var slice []int=[]int{
1,2,3}
// Print the results :slice: [1 2 3]
fmt.Printf("slice: %T\n", slice)
}
- A fourth : Create slices by referencing arrays
package main
import (
"fmt"
)
func main() {
// The form of a reference array
var arr [6]int = [...]int{
1, 2, 3, 4, 5, 6}
slice := arr[1:4]
// Print the results :slice: [2 3 4]
fmt.Printf("slice: %v\n", slice)
}
边栏推荐
猜你喜欢

Flink1.14 SQL basic syntax (I) detailed explanation of Flink SQL table query

Am I delayed by the code... Unfortunately, I became a programmer

漏风的小棉袄……

杂谈:高考

单臂路由(讲解+实验)

Use reflection to dynamically modify annotation attributes of @excel

Okaleido生态核心权益OKA,尽在聚变Mining模式

我是不是被代码给耽误了……不幸沦为一名程序员……

IDEA中文乱码怎么办

Chapter 6 Shell Logic and Arithmetic
随机推荐
ADC噪声全面分析 -02- ADC 噪声测量方法和相关参数
C语言 pthread_cleanup_push()和pthread_cleanup_pop()函数(用于临界资源程序段中发生终止动作后的资源清理任务,以免造成死锁,临界区资源一般上锁)
Shell condition test, judgment statement and operator of shell system learning
Docker install MySQL 8.0.28
Multi condition query of when
The DrawImage method calls the solution of not displaying pictures for the first time
Quickly update the information in a field in kettle
ARP broadcasting practice cases
Prior Attention Enhanced Convolutional Neural Network Based Automatic Segmentation of Organs at Risk
Jjwt generate token
2022-07-25 Gu Yujia's study notes
漏风的小棉袄……
sql语句批量更新 时间减去1天
单臂路由(讲解+实验)
Summary of several common ways to join dimension tables in Flink
基于Arduino的温度、湿度测量显示装置
Tcp/ip protocol analysis (tcp/ip three handshakes & four waves + OSI & TCP / IP model)
opengl-shader学习笔记:varying变量
Codeforces Round #810 (Div.2) A-C
网络入门——vlan及trunk概述