当前位置:网站首页>Go language - array
Go language - array
2022-06-11 15:45:00 【Crying while learning】
Preface
Array : A data structure that stores the same data type
The array is a fixed length , How big is defined . Manipulate data in an array , You can operate through indexes .
Create array syntax
Array
var array_name [size]array_type
var array_name = [size]array_type
var array_name = [size]array_type{v1, v2, v3, v4} // If the value is higher than size Small ,int Type use 0 fill ,string Type is filled with empty string .
var array_name = [size]array_type{index1: v1, index3 : v3}
//size It's written in ... The array length will be calculated automatically after the assignment is completed
array_name := [...]array_type{v1, v2, v3, v4}
array_name := [...]array_type{index1: v1, index3 : v3}
Multidimensional arrays
One dimensional array : Storing the data itself
a1 := [3]int{1,2,3}
Two dimensional array : What is stored in an array is also an array
a2 := [3][4]int{ {}, {}, {}}
Capacity 3 Is the capacity of a two bit array , Store 3 One dimensional array ; Capacity 4 Is the capacity of a one-dimensional array .
func main() {
arr := [3][4]int{
{}, {1, 2, 3, 4}, {}}
fmt.Println(arr)
}
// Output :[[0 0 0 0] [1 2 3 4] [0 0 0 0]]Access array
Array
Using the index of an array ( Subscript ) To access the array .
func main() {
array_test := []int{1, 2, 3, 4}
fmt.Println(array_test[0], array_test[3])
}Get the length and capacity of the array :len() To obtain the length of the 、cap() Capacity acquisition , The length and capacity of the array are equal .
Traversing a two-dimensional array
func main() {
arr := [2][4]int{
{}, {1, 2, 3, 4}}
fmt.Println("--- Array arr---: \n", arr)
//for loop
fmt.Println("\n---for Loop through groups arr---:")
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr[i]); j++ {
fmt.Printf("%d ", arr[i][j])
}
}
//for range
fmt.Println("\n---for range Traversal array arr---:")
for _, arr1val := range arr {
for _, arr2val := range arr1val {
fmt.Printf("%d ", arr2val)
}
}
}
Data type of array
Value type / Reference type
Arrays are value types . When you assign , Copy the original value to the new value . Operating the new value has no effect on the original value .
value type :int float bool string array
Reference type :slice map...
Reference type assignment , The new value is the memory address that directly manipulates the original value .
Array Memory analysis
Arrays store data of the same type , Will open up a continuous memory .go Language memory is automatically allocated by the system , When the array is not used , It will be by GC The garbage collection mechanism reclaims from memory .
Address of array , It is actually the address of the first element of the array . Because the element types in the array are the same , Use the same amount of memory . So it's easy to address .
func main() {
array_test := [4]int{1, 2, 3, 4}
fmt.Printf("%p-- Array memory address \n", &array_test)
fmt.Printf("%p-- No 1 Element memory address \n", &array_test[0])
fmt.Printf("%p-- No 2 Element memory address \n", &array_test[1])
}
边栏推荐
- [creation mode] factory method mode
- 05 _ 深入浅出索引(下)
- 05 _ In simple terms index (Part 2)
- Shuttle-- common commands
- 向数据库导入数据?试试COPY FROM STDIN语句
- 将配置导出到FTP或TFTP服务器
- 数据库密态等值查询概述及操作
- [system safety] XLII PowerShell malicious code detection series (4) paper summary and abstract syntax tree (AST) extraction
- 一文教会你数据库系统调优
- From 0 to 1, master the mainstream technology of large factories steadily. Isn't it necessary to increase salary after one year?
猜你喜欢

Don't you understand the design and principle of thread pool? Break it up and crush it. I'll teach you how to design the thread pool

openGauss 多线程架构启动过程详解

前沿科技探究之AI工具:Anomaly-detection

使用Cloud DB构建APP 快速入门-快游戏篇

Is it possible to use multiple indexes together in a query?

从0到1稳稳掌握大厂主流技术,年后涨薪不是必须的吗?
![How to write elegant secondary classification for enterprise development [small case of meituan]](/img/0b/d78b956b93d562d5e63d3fa398b040.png)
How to write elegant secondary classification for enterprise development [small case of meituan]

ASEMI的MOS管24N50参数,24N50封装,24N50尺寸

从屡遭拒稿到90后助理教授,罗格斯大学王灏:好奇心驱使我不断探索

AGC安全规则是如何简化用户授权和验证请求
随机推荐
C interface of learning notes
openGauss数据库性能调优概述及实例分析
【创建型模式】建造者模式
[creation mode] builder mode
Qcustomplot 1.0.1 learning (1) - Download and use qcustomplot
Is it possible to use multiple indexes together in a query?
轻松上手使用gs_dump和gs_dumpall命令导出数据
带你深度了解AGC云数据库
2022.02.28
openGauss数据库JDBC环境连接配置(Eclipse)
leetcode 120. 三角形最小路径和
同学,你听说过MOT吗?
I have used 20K for 5 years and met all major manufacturers. These high-frequency interview questions have helped you figure out
Arthas practice documentation
数据库资源负载管理(上篇)
Unified record of my code variable names
How about art plus online school? Is it a new online organization?
鼻孔插灯,智商上升,风靡硅谷,3万就成
Analysis on the architecture of distributed systems - transaction and isolation level (multi object, multi operation) Part 2
CF662B Graph Coloring题解--zhengjun