```
feat(array): 添加数组拷贝和遍历的学习内容 添加了数组拷贝的详细说明,包括值拷贝的概念和示例代码, 以及数组遍历的方法介绍。同时创建了相应的演示程序和 draw.io图表来可视化数组拷贝过程。 ```
This commit is contained in:
parent
565a810ba1
commit
f1fbed05d2
@ -48,5 +48,46 @@ arr[1] = 2
|
||||
arr[2] = 3
|
||||
```
|
||||
|
||||
## 子针数组(重要: 95%)
|
||||
## 数组拷贝
|
||||
|
||||
go 默认就是值拷贝, = 赋值 也是 值copy
|
||||
|
||||
1. 浅拷贝: 创建一个新数组,新数组的元素和原数组的元素是相同的,但是两个数组的元素是两个不同的对象
|
||||
|
||||
```go
|
||||
func main() {
|
||||
// ipv4 4字节表示一个ipv4地址, 简短声明: 127.0.0.1
|
||||
ip := [4]byte{127, 0, 0, 1}
|
||||
fmt.Println("ip1:", ip)
|
||||
|
||||
// 如何copy这个数组, 这个赋值有没有完成copy
|
||||
// 怎么理解数组的复制 是值拷贝
|
||||
//
|
||||
// a := 1
|
||||
// b := a
|
||||
// b = 2
|
||||
// fmt.Println("a:", a)
|
||||
// fmt.Println("b:", b)
|
||||
// 数组: 一片连续的地址控制
|
||||
// ip2 := ip (ip 有自己连续的地址空间)
|
||||
// ip2[4]byte{} (ip2 申请的连续地址)
|
||||
// Go 默认是值拷贝(重要)
|
||||
// ip 的值 传递给 ip2
|
||||
// ip2 和 ip 是两个不同的数组, 互不影响
|
||||
|
||||
ip2 := ip
|
||||
fmt.Println("ip2:", ip2)
|
||||
|
||||
// 修改ip2 不会影响ip
|
||||
ip2[0] = 10
|
||||
fmt.Println("ip1:", ip)
|
||||
fmt.Println("ip2:", ip2)
|
||||
}
|
||||
```
|
||||
|
||||
## 数组遍历
|
||||
|
||||
1. 循环遍历(for)
|
||||
```go
|
||||
|
||||
```
|
||||
53
day02/array/main.go
Normal file
53
day02/array/main.go
Normal file
@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
/*
|
||||
数组拷贝
|
||||
*/
|
||||
// ipv4 4字节表示一个ipv4地址, 简短声明: 127.0.0.1
|
||||
ip := [4]byte{127, 0, 0, 1}
|
||||
fmt.Println("ip1:", ip)
|
||||
|
||||
// 如何copy这个数组, 这个赋值有没有完成copy
|
||||
// 怎么理解数组的复制 是值拷贝
|
||||
//
|
||||
// a := 1
|
||||
// b := a
|
||||
// b = 2
|
||||
// fmt.Println("a:", a)
|
||||
// fmt.Println("b:", b)
|
||||
// 数组: 一片连续的地址控制
|
||||
// ip2 := ip (ip 有自己连续的地址空间)
|
||||
// ip2[4]byte{} (ip2 申请的连续地址)
|
||||
// Go 默认是值拷贝(重要)
|
||||
// ip 的值 传递给 ip2
|
||||
// ip2 和 ip 是两个不同的数组, 互不影响
|
||||
|
||||
ip2 := ip
|
||||
fmt.Println("ip2:", ip2)
|
||||
|
||||
// 修改ip2 不会影响ip
|
||||
ip2[0] = 10
|
||||
fmt.Println("ip1:", ip)
|
||||
fmt.Println("ip2:", ip2)
|
||||
|
||||
/*
|
||||
数组遍历
|
||||
*/
|
||||
// 1. 通用遍历法(不局限于数组, 切片, map, 字符串)
|
||||
// 对象的编号,通常叫索引, index (0, ...)
|
||||
// (0, 1, 2, 3)
|
||||
// 1. for : 定义变量(开始元素的索引,0), 条件(循环结束条件, 不满足条件,结束循环), 变化(索引加1, 找下一个元素)
|
||||
// len 函数: 求数组的长度
|
||||
// 127 0 0 1
|
||||
// for i := 0; i < len(ip); i++ {
|
||||
// fmt.Println(ip[i])
|
||||
// }
|
||||
// range 专用遍历法, 语法糖,更简洁, 自动处理索引的变化
|
||||
for i := range len(ip) {
|
||||
fmt.Println(ip[i])
|
||||
}
|
||||
|
||||
}
|
||||
46
day02/array/prt_array.drawio
Normal file
46
day02/array/prt_array.drawio
Normal file
@ -0,0 +1,46 @@
|
||||
<mxfile host="65bd71144e">
|
||||
<diagram id="-ptsffGBeFiTsS9EnCVV" name="第 1 页">
|
||||
<mxGraphModel dx="1134" dy="366" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
|
||||
<root>
|
||||
<mxCell id="0"/>
|
||||
<mxCell id="1" parent="0"/>
|
||||
<mxCell id="2" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="130" y="150" width="600" height="100" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="8" value="127" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="160" y="170" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="9" value="0" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="300" y="170" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="10" value="0" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="450" y="170" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="11" value="1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="590" y="170" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="17" value="IP 地址: [4]int64" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="70" y="110" width="120" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="18" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="130" y="330" width="600" height="100" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="19" value="IP2 地址: [4]int64" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
|
||||
<mxGeometry x="70" y="290" width="120" height="30" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="20" value="127" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="160" y="350" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="21" value="0" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="310" y="350" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="22" value="0" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="460" y="350" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
<mxCell id="23" value="1" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
|
||||
<mxGeometry x="600" y="350" width="120" height="60" as="geometry"/>
|
||||
</mxCell>
|
||||
</root>
|
||||
</mxGraphModel>
|
||||
</diagram>
|
||||
</mxfile>
|
||||
20
day02/struct/README.md
Normal file
20
day02/struct/README.md
Normal file
@ -0,0 +1,20 @@
|
||||
# 结构体
|
||||
|
||||
|
||||
## 子针数组(重要: 95%)
|
||||
|
||||
指针数组: 创建一个数组,里面存放指针,指针指向数组的某一个元素
|
||||
|
||||
盒子(数组)里面的插槽,嵌套盒子(指针)的模式
|
||||
|
||||
|
||||
```go
|
||||
type Person struct {}
|
||||
|
||||
//
|
||||
[]*Person{}
|
||||
```
|
||||
|
||||
|
||||
## 子针数组的copy
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user