Files
the-way-to-go_ZH_CN/eBook/18.2.md
Haigang Zhou fa1cfcc67f 第十七十八章 (#833)
Co-authored-by: Joe Chen <jc@unknwon.io>
2022-05-19 19:57:23 +08:00

51 lines
973 B
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 18.2 数组和切片
- 创建:
`arr1 := new([len]type)`
`slice1 := make([]type, len)`
- 初始化:
`arr1 := [...]type{i1, i2, i3, i4, i5}`
`arrKeyValue := [len]type{i1: val1, i2: val2}`
`var slice1 []type = arr1[start:end]`
1如何截断数组或者切片的最后一个元素
`line = line[:len(line)-1]`
2如何使用 `for` 或者 `for-range` 遍历一个数组(或者切片):
```go
for i:=0; i < len(arr); i++ {
= arr[i]
}
for ix, value := range arr {
}
```
3如何在一个二维数组或者切片 `arr2Dim` 中查找一个指定值 `V`
```go
found := false
Found: for row := range arr2Dim {
for column := range arr2Dim[row] {
if arr2Dim[row][column] == V{
found = true
break Found
}
}
}
```
## 链接
- [目录](directory.md)
- 上一节:[字符串](18.1.md)
- 下一节:[映射](18.3.md)