> For the complete documentation index, see [llms.txt](https://maxim218.gitbook.io/go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maxim218.gitbook.io/go/dinamicheskie-massivi.md).

# Динамические массивы

## Работа с динамическим массивом

Создадим функцию для считывания целого числа.

```go
func readInt(s string, x * int) {
    fmt.Print(s + ": ")
    var y int = 0
    fmt.Scan(&y)
    *x = y
}
```

Считаем с клавиатуры количество элементов.

```go
var n int = 0
readInt("Input number of elements", &n)
```

Создадим динамический массив.

```go
arr := make([]int, n)
```

Считываем элементы динамического массива.

```go
for i := 0; i < n; i++ {
   var k int = 0
   readInt("Input element", &k)
   arr[i] = k
}
```

Выводим элементы динамического массивы на экран.

```go
fmt.Print("Content of array: ")
for i := 0; i < n; i++ {
   var h int = arr[i]
   fmt.Print(h, "  ")
}
```

## Динамическое увеличение размера массива

Создадим пустой массив.

```go
arr := make([]int, 0)
fmt.Println(arr)
```

Считываем элементы массива.

Ввод оканчивается при введении нуля.

Каждый считанный элемент (кроме нуля) добавляется в конец массива.

```go
for true {
   var x int
   fmt.Scan(&x)
   if x == 0 {
      break
   }
   arr = append(arr, x)
}
```

Выводим сформированный массив на экран.

```go
fmt.Println(arr)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://maxim218.gitbook.io/go/dinamicheskie-massivi.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.
