4
4
4utka_pyan2017-06-23 17:37:08
go
4utka_pyan, 2017-06-23 17:37:08

What are arrays for in golang?

Judging by chapter golang-book.ru/chapter-06-arrays-slices-maps.html and practice
An array in golang has only a fixed length: I.e. you cannot create an array of indefinite length like var x []int for this there is a slice: In the textbook they write:
var x [5]int
var x []int

A slice is part of an array.

But in reality, a slice is not always part of an array, because it can exist separately from it and is in no way connected with it. Those. there can be a slice in the program and not a single array.
Question: why are arrays in the language made then?
Solely in order to create in advance some kind of list whose length is known, just to speed up the program (you don’t need to calculate either the length or look at the type), because arrays can’t be dynamically reduced or added to them? Can you just change?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2017-06-23
@4utka_pyan

If there is a slice in the program, then there is an array corresponding to it in memory. You don't create it yourself, but Goshka's runtime creates it.
The array is needed as a lower-level primitive, to speed up work in some cases, so as not to produce unnecessary pointers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question