Ich bin neu in der Go-Sprache. Ich habe mich mit den Datentypen array
und slice
verwechselt.Go lang Array und Slice Datentypen
Von Go docs, Arrays werden wie folgt beschrieben:
There are major differences between the ways arrays work in Go and C. In Go,
- Arrays are values. Assigning one array to another copies all the elements.
- In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it.
- The size of an array is part of its type. The types [10]int and [20]int are distinct.
Funktionen:
As in all languages in the C family, everything in Go is passed by value. That is, a function always gets a copy of the thing being passed, as if there were an assignment statement assigning the value to the parameter. For instance, passing an int value to a function makes a copy of the int, and passing a pointer value makes a copy of the pointer, but not the data it points to.
Warum sort.Ints(arrayValue)
die übergebene Variable nicht ändern, wenn ich es als ein Array deklariert, nicht als Scheibe?
-Code
var av = []int{1,5,2,3,7}
fmt.Println(av)
sort.Ints(av)
fmt.Println(av)
return
Ausgabe
[1 5 2 3 7]
[1 2 3 5 7]
Darüber hinaus wird sort.Ints Ihnen einen Fehler geben, wenn Sie es angaben Array statt einer Scheibe. Die Dokumentation für sort.Ints gibt ein Segment an. https://golang.org/pkg/sort/#Ints – Kirk
@Kirk guten Punkt. Ich habe es in die Antwort für mehr Sichtbarkeit aufgenommen. – VonC