F
F
Flie2015-04-05 14:27:29
go
Flie, 2015-04-05 14:27:29

How to work with pointers to arrays (slices) in GO (golang)?

Hey!
I have been studying GO for 2 whole days now, I wrote down a simple program for 200 lines. There is no question of any beauty in it, but "to try" is just right.
I really liked the goroutines (or goroutines or goroutines - there is no one to tell what it is called correctly).
As an obj-si_shnik, I was confused by the lack of arrays with an arbitrary size. (I was also confused by the presence of "slices" - it requires additional investigation) It was decided to write down the append function, which would take an array and what should be stuffed into it under the extra index (oh, generics, generics ...). I would like this function to take not the array itself, but only a pointer to it, but a problem immediately arises: a pointer to an array is received, but len ​​(this_array) is no longer possible to get - len does not accept a pointer. Tell me how to be here?
Maybe I'm digging in the wrong direction? Maybe there are arrays of arbitrary length somewhere in the go-subsoil?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
L
localghost, 2015-04-05
@Flie

I'm about your level in Go, but here's what I imagine: slices are "mutable arrays" for our purposes. In fact, these are references to arrays that are immutable anyway, but if we want to change the size of the slice, then we simply get a new underlying array and a reference to it.
For slices, there is a built-in append function:

s0 := []i n t {0, 0}
s1 := append(s0, 2)
s2 := append(s1, 3, 5, 7)
s3 := append(s2, s0...)

PS Almost everything I studied is this book: www.miek.nl/downloads/Go/Learning-Go-latest.pdf
PPS Goroutines, I think, are read like go-routines, with the main second accent :)

I
index0h, 2015-04-05
@index0h

Use slices, this is a reference type. If the size goes beyond the capacity, the slice's internal array will be recreated with a larger capacity.
In principle, you can also look at golang.org/pkg/container

U
uvelichitel, 2015-04-05
@uvelichitel

origArray := []string{"1", "2"}  //уже дает вам слайс, а не массив
origSlice := origArray[:]   //в этой строке нет никакого смысла
//массив можно получить так 
var myArray [2]string = [2]string{"1", "2"}

A slice in Go is a reference type. An object of this type contains three attributes len the length of an Int, cap the capacity of an Int, and a pointer to the first element of the Pointer.
All arguments in Go are passed by copying the value, that is, when you pass a function a slice, you pass a copy of the slice. This copy will contain its own len and cap, but still a pointer to the same first element as the argument. Thus, in order to modify the elements of a slice, it is enough to pass it to the function; in order to protect the slice from modification by the function, it is necessary to pass a copy of it made by the built-in function copy(dst, src) To give the function access to the length len and capacity cap of the slice, a pointer to it should be passed. append obviously requires changing not only the elements but also the length and capacity of the slice, so you need to pass a pointer to the slice.
foo(&origSlice){
*origSlice = append(* origSlice, "3")
...

However, be careful
foo (origSlice){
origSlice[1]="baz"
...
}

will change the original origSlice even though it cannot change its length

F
Flie, 2015-04-05
@Flie

index0h : I'm not very good at something. Let me try to explain in my own words what you need, and you, if possible, show how this is done in GO.
The first picture shows how you can work with pointers in Swift.
On the 2nd - how I try to do it in GO.
In the console -
[1 2]
[1 2]
if passed as &/* then an error on append
Tell me what's wrong?

D
DoZat0r, 2015-04-25
@DoZat0r

Good day.
As you were told earlier, this line makes no sense: origSlice := origArray[:]
In general, the solution is as follows:
98bfaddb9dd842828c22bad7ce58ce29.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question