# 数组和切片

创建：

`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
        }
    }
}
```

## 链接

* [目录](https://github.com/yangchuansheng/the-way-to-go_ZH_CN/tree/f30ab7d8c58f85840a0afb548024b93642b518d5/eBook/directory.md)
* 上一节：[字符串](/the-way-to-go-zh-cn/di-si-bu-fen-shi-ji-ying-yong/18.0/18.1.md)
* 下一节：[映射](/the-way-to-go-zh-cn/di-si-bu-fen-shi-ji-ying-yong/18.0/18.3.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryanyang.gitbook.io/the-way-to-go-zh-cn/di-si-bu-fen-shi-ji-ying-yong/18.0/18.2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
